2

Suppose I have an array like this

$array = ['a','b','c','d']

Now in order to see it on screen, i have two options:

var_dump or print_r

But their output is like

Array ( [0] => a [1] => b [2] => c [3] => d )

or

array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" }

I sometimes find it difficult to read.

Is there any way to get the output like

['a','b','c','d']

so that it's easy to read?

Jack Bonneman
  • 1,821
  • 18
  • 24
user3147180
  • 923
  • 3
  • 12
  • 25
  • 1
    @Barmar, He exactly wants it like that(how the array was constructed) and `var_export` wont do that. He wants that for reading purposes. – Shankar Narayana Damodaran Feb 22 '14 at 13:04
  • I concur with @Barmar - if you want to print actual PHP code then use `var_export`. You can assign the value to a variable instead of printing it if you pass `true` as the second argument, which is sometimes useful. Of course, the 5.4 short array syntax is sugar, what `var_export` will give you is the old `array()` style output. But that's readable too right? Alternatively, if you find the output of `var_dump` hard to read, I'd highly recommend XDebug which will nicely format your `var_dump` output: http://oi60.tinypic.com/1o84ex.jpg – Darragh Enright Feb 22 '14 at 13:23

5 Answers5

3

Seems like you are needing it for reading purposes.. for that make use of json_encode();

<?php
$array = ['a','b','c','d'];
echo json_encode($array);  //"prints" ["a","b","c","d"]
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

You can make your own function:

function print_array($array) {
   echo '[';
   foreach($array as $letter) {
      echo $letter . ',';
   }
   echo ']';
}

$arr = ['a','b','c','d'];
print_array($arr);
aksu
  • 5,221
  • 5
  • 24
  • 39
0

you should add

    <pre></pre>

tag before and after print_r function and suparet them

Miki Maine Amdu
  • 199
  • 1
  • 12
-1

I use

echo "<pre>".print_r($array,1)."</pre>";
Ronnie Jespersen
  • 950
  • 2
  • 9
  • 22
  • This is not printing result what OP is wanting. He is already tried print_r. – aksu Feb 22 '14 at 13:10
  • 1
    No but the formatting is good for reading... and thats basically what he is asking for. – Ronnie Jespersen Feb 22 '14 at 13:11
  • He said this: *Is there any way to get the output like ['a','b','c','d']...* He wants to display the array in that format. – aksu Feb 22 '14 at 13:13
  • I still don't agree :) The user is asking for a readable format. Not knowing that he cant get the by using the code I provided. Its not a matter of format. That was the suggestion by him self. My answer is still solving the core issue readability. Even @Philipp answered two minutes after with the same answer.. But okay that's your take on it :) – Ronnie Jespersen Feb 22 '14 at 13:17
-1

The output of var_dump or print_r is readable - the problem is the html output, which removes line-breaks.

So I suggest to use this functions inside a pre-tag

echo '<pre><code>';
var_dump($data);
echo '</code></pre>';

Also you could install the php module xdebug which could modify the output of var_dump for better readability: http://xdebug.org/docs/display

Philipp
  • 15,377
  • 4
  • 35
  • 52