Sorry for the length of this post - this is more like a mini tutorial, but hopefully this should give you some good concepts to help you solve this problem. There are several approaches that I would recommend.
Tip # 1 , when you use print_r, try using it like this:
print_r ($array[query], 1);
This will allow you to add a “return” by setting the return flag to true. The advantage of this is that you can embed it like this:
<pre> <?php echo (print_r($array[query], 1)); ?> </pre>
This will print a “pre-formatted” array into your HTML code, which will save all spaces and line breaks. See http://php.net/manual/en/function.print-r.php for details. I will not go into details on how to do this, but there are a few tutorials here that will help you get started: http://www.java2s.com/Code/Php/Data-Structure/LoopingThroughaMultidimensionalArray.htm (also http: / /php.net/manual/en/control-structures.foreach.php )
Here is a simple example using the object code above:
Tip number 2 . Often I find that when I work with the API and "arrays" derived from the database results, the type is actually erroneous. For example, you often get something that looks like an array, but it's actually a stdObject. Even if it is not, I would advise you to try this function (from http://php.net/manual/en/function.var-dump.php ):
<?php $a = array(1, 2, array("a", "b", "c")); var_dump($a); ?>
Objects and arrays perform similar actions, but you can avoid notifications and possible headaches using this approach, since var_dump also displays the type and length of the object. You may try:
<?php $book = new stdClass; $book->title = "Harry Potter and the Prisoner of Azkaban"; $book->author = "JK Rowling"; $book->publisher = "Arthur A. Levine Books"; $book->amazon_link = "http://rads.stackoverflow.com/amzn/click/0439136369"; ?> <pre> <?php ob_start(); var_dump($book); $a = ob_get_clean(); $b = print_r($book,1); echo($a."\n\n".$b); ?> </pre>
It also adds output buffering, which can affect performance, but I saved several hours of debugging from deviations (you will also want to use something like Zend-debug or Xdebug). There is a bit of buffering PHP output with var_dump: How can I get the var_dump result for a string?