InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP

    PHP strings

    • PHP - split string examples
    • PHP - take last n characters of a string
    • PHP check if string contains something

    PHP Array

    • PHP - associative array value in double quoted string
    • PHP - check if a value is in array
    • PHP - convert array to associative array
    • PHP - print array in one line
    • PHP array foreach - code snippets
    • PHP array map example
    • PHP check if key exists in array
    • PHP sort associative array using custom compare function
    • php get array value with default

    PHP Regex

    • PHP Regex

    PHP Cookbook

    • PHP - assign multi lines string to a variable
    • PHP - call a function with arguments in array examples
    • PHP - get calling file name and line number using debug_backtrace
    • PHP - get class name and file name from an object
    • PHP - get function arguments examples
    • PHP - how to catch errors using set_error_handler
    • PHP - how to initialize static variables
    • PHP - how to use function static variable as cache
    • PHP check if a filename is valid
    • PHP elapsed time
    • PHP execute command and capture shell exit status
    • PHP how to get current url
    • PHP print number to two decimal places
    • PHP remove trailing whitespaces and newline
    • Php equal (==) vs identical (===)
    • Running php eval on code with tags

    PHP functions online

    • PHP functions online
     
    • Home
    • > Tutorials
    • > PHP

    PHP – print array in one line

    By admin on Dec 12, 2015

    At times for large array values we want to log them in one line for better readability. Logging an array in one line is also grep friendly. Here are few approached and php code snippets.

    Print array in one line using print_r and str_replace

    <?php
    function my_print_r ($x) {
      return str_replace(PHP_EOL, '', print_r($x, TRUE));
    }
    $a = array('a' => "val\na", 'b' => 'valb');
    echo "=====Before removing newline=====\n";
    print_r($a);
    echo "=====After removing newline=====\n";
    echo my_print_r($a);
    ?>
    =====Before removing newline=====
    Array
    (
        [a] => val
    a
        [b] => valb
    )
    =====After removing newline=====
    Array(    [a] => vala    [b] => valb)
    Env: PHP 7.3.18 (Linux)

    Print array in one line using json_encode

    We’ll php PHP_EOL for replacing all newlines.

    <?php
    function my_print_r2 ($x) {
      return json_encode($x);
    }
    $a = array('a' => "val\na", 'b' => 'valb');
    echo "=====Before removing newline=====\n";
    print_r($a);
    echo "=====After removing newline=====\n";
    echo my_print_r2($a);
    ?>
    =====Before removing newline=====
    Array
    (
        [a] => val
    a
        [b] => valb
    )
    =====After removing newline=====
    {"a":"val\na","b":"valb"}
    Env: PHP 7.3.18 (Linux)

    Suggested posts:

    1. php get array value with default
    2. Bash associative array tutorial
    3. python print examples
    4. Javascript – print all methods of an object
    5. Mac – print java home on command line
    6. PHP – convert array to associative array
    7. PHP array map example
    8. PHP array foreach – code snippets
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged PHP, PHP arrays, PHP cookbook, Tutorials, Web Development

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2023 InfoHeap.

    Powered by WordPress