How to echo or print an array in PHP?

https://stackoverflow.com/questions/9816889/how-to-echo-or-print-an-array-in-php

18549

I have this array

Array
(
  [data] => Array
    (
      [0] => Array
        (
          [page_id] => 204725966262837
          [type] => WEBSITE
        )

      [1] => Array
        (
          [page_id] => 163703342377960
          [type] => COMMUNITY
        )
      )
)

My question is how can I just echo the content without this structure? I tried

phparrow-up-right arraysarrow-up-rightsharearrow-up-rightimprove this questionarrow-up-rightedited May 31 '19 at 23:48arrow-up-rightarrow-up-rightSherylHohmanarrow-up-right8,7201111 gold badges5252 silver badges6666 bronze badgesasked Mar 22 '12 at 5:22arrow-up-rightEnexoOnomaarrow-up-right6,8281616 gold badges7474 silver badges154154 bronze badgesadd a commentarrow-up-right

11 Answers

activearrow-up-rightoldestarrow-up-rightvotesarrow-up-right111

This will do

sharearrow-up-rightimprove this answerarrow-up-rightanswered Mar 22 '12 at 5:29arrow-up-rightShiplu Mokaddimarrow-up-right49.1k1212 gold badges114114 silver badges174174 bronze badges

add a commentarrow-up-right441

To see the contents of array you can use.

1) print_r($array); or if you want nicely formatted array then:

2) use var_dump($array) to get more information of the content in the array like datatype and length.

3) you can loop the array using php's foreach(); and get the desired output. more info on foreach in php's documentation website: http://in3.php.net/manual/en/control-structures.foreach.phparrow-up-rightsharearrow-up-rightimprove this answerarrow-up-rightedited Feb 4 '16 at 17:34arrow-up-rightarrow-up-rightweb-tikiarrow-up-right79.8k2525 gold badges182182 silver badges218218 bronze badgesanswered Mar 22 '12 at 5:32arrow-up-rightIbrahim Azhar Armararrow-up-right21.3k2929 gold badges106106 silver badges179179 bronze badges

add a commentarrow-up-right91

If you just want to know the content without a format (e.g. for debuging purpose) I use this:

This will show it as a JSON which is pretty human readable.sharearrow-up-rightimprove this answerarrow-up-rightedited Jan 7 '14 at 2:07arrow-up-rightanswered Jun 28 '13 at 19:11arrow-up-rightMark Earrow-up-right2,6351515 silver badges3131 bronze badges

add a commentarrow-up-right29

There are multiple function to printing array content that each has features.

Prints human-readable information about a variable.

Displays structured information about expressions that includes its type and value.

Displays structured information about the given variable that returned representation is valid PHP code.

Note that because browser condense multiple whitespace characters (including newlines) to a single space (answerarrow-up-right) you need to wrap above functions in <pre></pre> to display result in correct format.

Also there is another way to printing array content with certain conditions.

Output one or more strings. So if you want to print array content using echo, you need to loop through array and in loop use echo to printing array items.

sharearrow-up-rightimprove this answerarrow-up-rightanswered Oct 14 '18 at 8:51arrow-up-rightMohammadarrow-up-right17.7k1313 gold badges4343 silver badges6868 bronze badgesadd a commentarrow-up-right20

Did you try using print_rarrow-up-right to print it in human-readable form?sharearrow-up-rightimprove this answerarrow-up-rightedited Jul 15 '15 at 14:50arrow-up-rightarrow-up-rightkamal palarrow-up-right4,05955 gold badges2020 silver badges3939 bronze badgesanswered Mar 22 '12 at 5:26arrow-up-rightWalkerarrow-up-right1,14522 gold badges1212 silver badges2626 bronze badgesadd a commentarrow-up-right19

You can use print_rarrow-up-right, var_dumparrow-up-right and var_exportarrow-up-right funcations of php:

print_r: Convert into human readble form

var_dump(): will show you the type of the thing as well as what's in it.

foreach loop: using for each loop you can iterate each and every value of an array.

sharearrow-up-rightimprove this answerarrow-up-rightedited Aug 21 '19 at 8:23arrow-up-rightanswered Aug 15 '15 at 3:30arrow-up-rightAnkur Tiwariarrow-up-right2,40622 gold badges1313 silver badges3838 bronze badgesadd a commentarrow-up-right17

or echo $results['data'][1]['type'];sharearrow-up-rightimprove this answerarrow-up-rightedited Mar 22 '12 at 5:47arrow-up-rightanswered Mar 22 '12 at 5:25arrow-up-rightRezignedarrow-up-right4,11211 gold badge1616 silver badges1717 bronze badges

add a commentarrow-up-right8

You have no need to put for loop to see the data into the array, you can simply do in following manner

sharearrow-up-rightimprove this answerarrow-up-rightedited Aug 2 '17 at 20:30arrow-up-rightarrow-up-rightBlackcoat77arrow-up-right1,32411 gold badge1414 silver badges2525 bronze badgesanswered Jul 15 '15 at 12:53arrow-up-rightVinod Kirtearrow-up-right18111 silver badge66 bronze badgesadd a commentarrow-up-right3

I know this is an old question but if you want a parseable PHP representation you could use:

If you echo the exported code to a file.php (with a return statement) you may require it as

sharearrow-up-rightimprove this answerarrow-up-rightanswered Jan 3 '17 at 14:31arrow-up-rightNiclasarrow-up-right1,20633 gold badges1414 silver badges2525 bronze badgesadd a commentarrow-up-right0

I checked the answer however, (for each) in PHP is deprecated and no longer work with the latest php versions.

Usually we would convert an array into a string to log it somewhere, perhaps debugging or test etc.

I would convert the array into a string by doing:

$Output = implied (",",$SourceArray);

Whereas:

$output is the result (where the string would be generated

",": is the separator (between each array field

$SourceArray: is your source array.

I hope this helpssharearrow-up-rightimprove this answerarrow-up-rightanswered Jul 9 '19 at 10:48arrow-up-rightHeider Satiarrow-up-right1,4481313 silver badges2020 bronze badgesadd a commentarrow-up-right-2

Loop through and print all the values of an associative array, you could use a foreach loop, like this:

sharearrow-up-rightimprove this answerarrow-up-rightanswered Apr 25 '18 at 8:50arrow-up-rightA. Moralesarrow-up-right651212 bronze badgesadd a commentarrow-up-rightHighly active questionarrow-up-right. Earn 10 reputation in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged phparrow-up-right arraysarrow-up-right or ask your own questionarrow-up-right.

Last updated