How-To Geek
Printing the contents of a PHP Array
This is a pretty common task, but it’s surprising how many PHP newbies don’t know about it yet. If you want to see the structure of your PHP Array, all you need to do is this:
print_r($myarray);
This will give you a listing of all the items in the array, including multi-dimensional arrays. Obviously this should only be used for testing / debugging purposes.
|
Subscribe |
Daily Email Updates |
|
You can get our how-to articles in your inbox each day for free. Just enter your email below: |
- By The Geek on 09/26/06
Comments (18)
Comments are closed on this post.
If you'd like to continue the discussion on this topic, you can do so at our forum.
Go to the Forum

and if you like it human-readable:
echo “”; print_r($myarray); echo “”;
have fun!
this is more human-readable :
echo ”;
print_r($array);
echo ”;
You may want to try this:
echo “”;
print_r($location);
echo “”;
echo “(html open tag)pre(html close tag)”;
print_r($location);
echo “\”;
echo “(less than sign)pre(greater than sign)”;
print_r($location);
echo “(less than sign)/pre(greather than sign)”;
between the quotes are the html pre tags (open and then close).
surprising how very helpful this post is for php newbies like me :)
thx alot!
This /is/ super helpful for PHP newbies like me.
Thanks!
if you want to print arrays with html, do it with this
echo ”.htmlspecialchars(print_r($inputs, TRUE)).”;
Derrick, what are “.htmlspecialchars”?
@tfq: It converts >, <, and several other HTML characters into their symbol codes: >, <, and so on.
I wrote a function for you guys if you are interested,
just save it in a helpers.php, include that file and you’re ready to go :)
Enjoy!
<?php
/** Prints the contents of an array with HTML special characters, linebreaks and spaces
* @param array $array
* @param bool $echo (default: true)
* @result bool/array
*/
function print_arr($array, $echo = true)
{
$array = print_r($array, true);
$array = htmlspecialchars($array);
$array = str_replace(" ", " ", $array);
$array = nl2br("”.$array.”");
if($echo == true)
{
echo $array;
return true;
}
return $array;
}
# use like this to print/echo the contents to the screen
print_arr($yourArray);
# use like this to not print/echo but catch the contents in a variable
$result = $print_arr($yourArray);
?>
Oops, hit enter a tad to fast in that one,
For not printing it to thescreen, you should use
$result = $print_arr($yourArray, false);
Sorry for the thrid post but I see that this comment system, but my space special character in the str_replace also turned to a real space, just you “$array = str_replace(” “, “& n b s p ;”, $array);” without the spaces inbetween the code.
echo “”;
print_r($arrayVar);
echo “”;
its help u much i think
Also try:
var_dump($myArray);
I have this situation:
$name = “my name”;
$surname = “my surname”;
$data['email'] = array(‘NAME’ => $name, ‘SURNAME’ => $surname);
How to make labels and strings content be printed like this?
NAME: my name
SURNAME: my surname
Someone can help me please? tks
Thanks, PastulioLive. The function worked great. You da’ man.