25

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.

9 Answers 9

35

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().

Sign up to request clarification or add additional context in comments.

Comments

19

For nice & readable results, use this:

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

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

2 Comments

Shouldn't you call that printVar or debugVar? Why call it readVar?
Good point, named it on the fly. Should probably be printVar. Thanks for the down-vote.
6

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.

http://php.net/manual/en/function.var-dump.php

Comments

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.

Comments

4

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){ if(is_array($array)){ echo $title."<br/>". "||---------------------------------||<br/>". "<pre>"; print_r($array); echo "</pre>". "END ".$title."<br/>". "||---------------------------------||<br/>"; }else{ echo $title." is not an array."; } } 

Basic usage:

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

Results:

PETS ||---------------------------------|| Array ( [0] => cat [1] => dog [2] => bird [3] => mouse [4] => fish [5] => gerbil ) END PETS ||---------------------------------|| 

Comments

3

You can use print_r to get human-readable output.

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

Comments

1

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>'; print_r($var); echo '</pre>'; } 

Comments

0
 //@parram $data-array,$d-if true then die by default it is false //@author Your name function p($data,$d = false){ echo "<pre>"; print_r($data); echo "</pre>"; if($d == TRUE){ die(); } } // END OF FUNCTION 

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. } 

Comments

0

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

echo '<pre>'; print_r($row); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.