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
foreach ($results as $result) {
echo $result->type;
echo "<br>";
}
php arraysshareimprove this questionedited May 31 '19 at 23:48SherylHohman8,7201111 gold badges5252 silver badges6666 bronze badgesasked Mar 22 '12 at 5:22
EnexoOnoma6,8281616 gold badges7474 silver badges154154 bronze badgesadd a comment
11 Answers
This will do
foreach($results['data'] as $result) {
echo $result['type'], '<br>';
}
shareimprove this answeranswered Mar 22 '12 at 5:29Shiplu Mokaddim49.1k1212 gold badges114114 silver badges174174 bronze badges
Thank you for this. Can you tell me how can I echo for example only the type of the Array [1] ? – EnexoOnoma Mar 22 '12 at 5:29
6@Kaoukkos
echo $results['data'][1]['type'];
– Shiplu Mokaddim Mar 22 '12 at 5:33What if I dont have any key like
data
? – Pratik Butani Oct 24 '18 at 11:10
To see the contents of array you can use.
1) print_r($array);
or if you want nicely formatted array then:
echo '<pre>'; print_r($array); echo '</pre>';
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.phpshareimprove this answeredited Feb 4 '16 at 17:34web-tiki79.8k2525 gold badges182182 silver badges218218 bronze badgesanswered Mar 22 '12 at 5:32
Ibrahim Azhar Armar21.3k2929 gold badges106106 silver badges179179 bronze badges
1Nice and elegant. You might want to change the closing tag in #1 from <pre/> to </pre>. – Michael Jun 21 '14 at 21:47
1This answer does not address the mistake that OP did. – Shiplu Mokaddim Nov 21 '14 at 17:28
2could you explain how the <pre></pre> construct makes this display "nicely?" – Robin Andrews May 28 '16 at 17:04
1@Robin <pre> tag displays new lines and tabulation as it's outputed by print_r(); without <pre> you would see a messy unformatted bounds of data. To see it formatted you should then view the html page source. – j.c Sep 29 '16 at 9:00
If you just want to know the content without a format (e.g. for debuging purpose) I use this:
echo json_encode($anArray);
This will show it as a JSON which is pretty human readable.shareimprove this answeredited Jan 7 '14 at 2:07answered Jun 28 '13 at 19:11Mark E2,6351515 silver badges3131 bronze badges
I wanted to extract some data from a
html - php
document using CordovaInAppBrowser
executeScript
method, withoutjson_encode($array)
I could not achieve that! Thanks a lot @Mark E – Hamid Araghi Mar 29 '19 at 11:42
There are multiple function to printing array content that each has features.
Prints human-readable information about a variable.
$arr = ["a", "b", "c"];
echo "<pre>";
print_r($arr);
echo "</pre>";
Array
(
[0] => a
[1] => b
[2] => c
)
Displays structured information about expressions that includes its type and value.
echo "<pre>";
var_dump($arr);
echo "</pre>";
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
Displays structured information about the given variable that returned representation is valid PHP code.
echo "<pre>";
var_export($arr);
echo "</pre>";
array (
0 => 'a',
1 => 'b',
2 => 'c',
)
Note that because browser condense multiple whitespace characters (including newlines) to a single space (answer) 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.
foreach ($arr as $key=>$item){
echo "$key => $item <br>";
}
0 => a
1 => b
2 => c
shareimprove this answeranswered Oct 14 '18 at 8:51Mohammad17.7k1313 gold badges4343 silver badges6868 bronze badgesadd a comment20
Did you try using print_r
to print it in human-readable form?shareimprove this answeredited Jul 15 '15 at 14:50kamal pal4,05955 gold badges2020 silver badges3939 bronze badgesanswered Mar 22 '12 at 5:26
Walker1,14522 gold badges1212 silver badges2626 bronze badgesadd a comment19
You can use print_r
, var_dump
and var_export
funcations of php:
print_r
: Convert into human readble form
<?php
echo "<pre>";
print_r($results);
echo "</pre>";
?>
var_dump()
: will show you the type of the thing as well as what's in it.
var_dump($results);
foreach loop
: using for each loop you can iterate each and every value of an array.
foreach($results['data'] as $result) {
echo $result['type'].'<br>';
}
shareimprove this answeredited Aug 21 '19 at 8:23answered Aug 15 '15 at 3:30Ankur Tiwari2,40622 gold badges1313 silver badges3838 bronze badgesadd a comment17
foreach($results['data'] as $result) {
echo $result['type'], '<br />';
}
or echo $results['data'][1]['type'];
shareimprove this answeredited Mar 22 '12 at 5:47answered Mar 22 '12 at 5:25Rezigned4,11211 gold badge1616 silver badges1717 bronze badges
Thank you for this. Can you tell me how can I echo for example only the type of the Array [1] ? – EnexoOnoma Mar 22 '12 at 5:29
what do you mean by Array [1]? :o – Andreas Wong Mar 22 '12 at 5:32
@andreas I want to echo only the COMMUNITY or the 163703342377960. Not the entire content of the array but specifically – EnexoOnoma Mar 22 '12 at 5:35
Try
echo $results['data'][1]['page_id'];
– Andreas Wong Mar 22 '12 at 5:36
You have no need to put for loop to see the data into the array, you can simply do in following manner
<?php
echo "<pre>";
print_r($results);
echo "</pre>";
?>
shareimprove this answeredited Aug 2 '17 at 20:30Blackcoat771,32411 gold badge1414 silver badges2525 bronze badgesanswered Jul 15 '15 at 12:53
Vinod Kirte18111 silver badge66 bronze badgesadd a comment3
I know this is an old question but if you want a parseable PHP representation you could use:
$parseablePhpCode = var_export($yourVariable,true);
If you echo the exported code to a file.php (with a return statement) you may require it as
$yourVariable = require('file.php');
shareimprove this answeranswered Jan 3 '17 at 14:31Niclas1,20633 gold badges1414 silver badges2525 bronze badgesadd a comment0
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 helpsshareimprove this answeranswered Jul 9 '19 at 10:48Heider Sati1,4481313 silver badges2020 bronze badgesadd a comment-2
Loop through and print all the values of an associative array, you could use a foreach
loop, like this:
foreach($results as $x => $value) {
echo $value;
}
shareimprove this answeranswered Apr 25 '18 at 8:50A. Morales651212 bronze badgesadd a commentHighly active question. 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 php arrays or ask your own question.
Last updated
Was this helpful?