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:



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.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/26/06

Comments (18)

  1. sven

    and if you like it human-readable:
    echo “”; print_r($myarray); echo “”;

    have fun!

  2. Magic.Crazy

    this is more human-readable :

    echo ”;
    echo ”;

  3. Hmmmm

    You may want to try this:

    echo “”;
    echo “”;

  4. Hmmmm

    echo “(html open tag)pre(html close tag)”;
    echo “\”;

  5. Hmmmm

    echo “(less than sign)pre(greater than sign)”;
    echo “(less than sign)/pre(greather than sign)”;

  6. Hmmmm

    between the quotes are the html pre tags (open and then close).

  7. jeroen

    surprising how very helpful this post is for php newbies like me :)
    thx alot!

  8. Jhm718

    This /is/ super helpful for PHP newbies like me.


  9. Derrick

    if you want to print arrays with html, do it with this

    echo ”.htmlspecialchars(print_r($inputs, TRUE)).”;

  10. tfq

    Derrick, what are “.htmlspecialchars”?

  11. wowaname

    @tfq: It converts >, <, and several other HTML characters into their symbol codes: &gt;, &lt, and so on.

  12. PastulioLive

    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 :)



    /** 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

    # use like this to not print/echo but catch the contents in a variable
    $result = $print_arr($yourArray);


  13. PastulioLive

    Oops, hit enter a tad to fast in that one,
    For not printing it to thescreen, you should use

    $result = $print_arr($yourArray, false);

  14. PastulioLive

    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.

  15. Saran

    echo “”;
    echo “”;

    its help u much i think

  16. Mikey

    Also try:


  17. Civali

    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

  18. jeff

    Thanks, PastulioLive. The function worked great. You da’ man.

More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!