0

I used the print_r function to print array fetched from a table in database. Instead of a nicely formatted array like this:

Array
(
    [0] => Array
        (
            [id] => 1
            [firstname] => Firstname one
            [lastname] => Lastname one
        )

    [1] => Array
        (
            [id] => 2
            [firstname] => Firstname two
            [lastname] => Lastname two
        )

    [2] => Array
        (
            [id] => 3
            [firstname] => Firstname three
            [lastname] => Lastname three
        )
)

I got inline results, like this:

Array ([0] => Array ([id] => 1 [firstname] => Firstname one [lastname] => Lastname one ) [1] => Array ([id] => 2 [firstname] => Firstname two...

This is a simple example, my actual array is nested three times, so the result is a massive block of inline string that makes readability hard.

Why do I get results such as this?

escplat12
  • 2,191
  • 4
  • 22
  • 34
  • 1
    Echo
     before and 
    after the print_r
    – Lelio Faieta Jan 30 '18 at 21:47
  • To expand on that comment a bit -- `print_r()` doesn't generate HTML, it generates plain text. If you want it to display nicely in a browser, you have to add markup. – Alex Howansky Jan 30 '18 at 21:48
  • 1
    Why? Because you're printing on a web page, and the browser is re-formatting just like it does any other text. – Barmar Jan 30 '18 at 21:48
  • `var_dump` , `print_r` and maybe better `var_export` https://stackoverflow.com/questions/19816438/make-var-dump-look-pretty/19816742#19816742 can use with `highlight_string`. – AbraCadaver Jan 30 '18 at 21:50

1 Answers1

9

This happens simply because you are printing plaintext straight to your webpage, which automatically reformats text, luckily there is a solution.

The following code will "pretty print" your PHP arrays.

echo "<pre>".print_r($array, true)."</pre>";

The HTML <pre> tag is used for indicating preformatted text. The code tag surrounds the code being marked up.

https://www.tutorialspoint.com/html/html_pre_tag.htm

The 2nd parameter of print_r() tells the function to capture the output of print_r() instead of printing it as plaintext. Learn more on the PHP docs.

These two methods put together make the output to be "prettified".


As noted by dognose in the comments, if you don't want to use <pre> tags for whatever reason, you can simply view the source code of the page you are working on and it will show the array in a "nice" fashion.

Example posted by dognose: enter image description here


Function

I've written a little pretty print function that will allow you to do this a bit easier...

function print_p($arr) {
    return "<pre>".print_r($arr, true)."</pre>";
}

This can save you a lot of time/effort of typing the pre tags every time. Usage:

echo print_p($array);
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
  • 1
    Or - if you want to avoid the `
    `-tags, just check the source code of the page. It will show the array in a "nice" fashion, without `
    `.
    – dognose Jan 30 '18 at 21:55
  • @dognose Hope you don't mind, I added that to my answer. – GrumpyCrouton Jan 30 '18 at 21:57
  • Excellent, thanks! Will accept the answer in a minute :) – escplat12 Jan 30 '18 at 21:58
  • @GrumpyCrouton The "whatever reason" is: it's more time consuming to type the String-Concatenation, than do `print_r($arr)` followed by a "right click" -> "Inspect Element" in Chrome :P – dognose Jan 30 '18 at 22:05
  • I'm sure there are other reasons too @dognose :) and in that case, why not just make a new function that includes the pretags? – GrumpyCrouton Jan 31 '18 at 13:08
  • @GrumpysaysReinstateMonica its not possible to return an echo direct from the function and everytime just to call the function ? – Cananau Cristian Nov 21 '19 at 15:03
  • @CananauCristian I'm sorry, I don't understand what you are asking. I think you are asking if you can just use `echo` inside the function, if so, the answer is yes. I generally always use `return` though. – GrumpyCrouton Dec 02 '19 at 14:12
  • @GrumpysaysReinstateMonica noo i mean realy "return echo $array;" soo that you can use that only with print_p($array) – Cananau Cristian Dec 02 '19 at 14:28
  • @CananauCristian You can't return an echo, but you could just do `echo "
    ".print_r($arr, true)."
    ";` inside the function so you don't have to use `echo` when you call the function.
    – GrumpyCrouton Dec 02 '19 at 14:30