Output (echo/print) everything from a PHP Array

Is it possible to echo or print the entire contents of an array without specifying which part of the array?

The scenario: I am trying to echo everything from:

while($row = mysql_fetch_array($result)){
echo $row['id'];

Without specifying “id” and instead outputting the complete contents of the array.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

If you want to format the output on your own, simply add another loop (foreach) to iterate through the contents of the current row:

while ($row = mysql_fetch_array($result)) {
    foreach ($row as $columnName => $columnData) {
        echo 'Column name: ' . $columnName . ' Column data: ' . $columnData . '<br />';

Or if you don’t care about the formatting, use the print_r function recommended in the previous answers.

while ($row = mysql_fetch_array($result)) {
    echo '<pre>';
    print_r ($row);
    echo '</pre>';

print_r() prints only the keys and values of the array, opposed to var_dump() whichs also prints the types of the data in the array, i.e. String, int, double, and so on. If you do care about the data types – use var_dump() over print_r().

Solution 2

For nice & readable results, use this:

function printVar($var) {
    echo '<pre>';
    echo '</pre>';

The above function will preserve the original formatting, making it (more)readable in a web browser.

Solution 3

var_dump() can do this.

This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.


Solution 4

I think you are looking for print_r which will print out the array as text. You can’t control the formatting though, it’s more for debugging. If you want cool formatting you’ll need to do it manually.

Solution 5

This is a little function I use all the time its handy if you are debugging arrays. Its pretty much the same thing Darryl and Karim posted. I just added a parameter title so you have some debug info as what array you are printing. it also checks if you have supplied it with a valid array and lets you know if you didn’t.

function print_array($title,$array){


            echo $title."<br/>".
            echo "</pre>".
            "END ".$title."<br/>".

             echo $title." is not an array.";

Basic usage:

//your array
$array = array('cat','dog','bird','mouse','fish','gerbil');
print_array("PETS", $array);



    [0] => cat
    [1] => dog
    [2] => bird
    [3] => mouse
    [4] => fish
    [5] => gerbil


Solution 6

You can use print_r to get human-readable output.

See http://www.php.net/print_r

Solution 7

Similar to karim’s, but with print_r which has a much small output and I find is usually all you need:

function PrintR($var) {
    echo '<pre>';
    echo '</pre>';

Solution 8

 //@parram $data-array,$d-if true then die by default it is false
 //@author Your name

 function p($data,$d = false){

     echo "<pre>"; 
     echo "</pre>"; 

     if($d == TRUE){

Use this function every time whenver you need to string or array it will wroks just GREAT.

There are 2 Patameters

1.$data – It can be Array or String
2.$d – By Default it is FALSE but if you set to true then it will execute die() function

In your case you can use in this way….

while($row = mysql_fetch_array($result)){
    p($row); // Use this function if you use above function in your page.

Solution 9

You can use print_r to get human-readable output.
But to display it as text we add echo '<pre>';

echo '<pre>';

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply