Array Debugging: Nicer print_r

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Array Debugging: Nicer print_r

Post by pickle »

Here's a little function I wrote because I found the default print_r was useless when viewing large, nested arrays. Enjoy!

Code: Select all

function ordered_print_r($p_array,$current_key = '')
{
  $current_key = ($current_key != '')? "$current_key => " : '';

  echo <<<TABLE
<table>
  <tr>
    <td>
      $current_key<strong>Array(</strong>
    </td>
  </tr>
TABLE;

  while($row = each($p_array))
  {
    echo <<<ELEMENT
  <tr>
    <td>
      &nbsp;//put a couple non-breaking spaces here
      &nbsp;//put a couple non-breaking spaces here
    </td>
    <td>
ELEMENT;
    if(gettype($row[value]) == "array")
    {
      echo "<td><td>".ordered_print_r($row[value],$row[key])."</td>";
    }
    else
    {
      echo  <<<ROW
    <td>
      $row[key]
    </td>
    <td>
      =>$row[value]
    </td>
ROW;
    }
echo <<<ELEMENT
  </tr>
ELEMENT;
  }
  echo <<<TABLEEND
  <tr>
    <th align = "right">
      )
    </th>
  </tr>
</table>
TABLEEND;
}
Obvously there can be some formatting that can be done to taste.

Here's an example of the output

Code: Select all

$array[0] = "orange";
$array[1] = array("yellow"=>"banana","red"=>"apple","green"=>"another apple");

ordered_print_r($array);
Will return:

Code: Select all

=> Array(  
                0 =>orange 
                1 => Array(  
                                  yellow =>banana 
                                  red =>apple 
                                  green =>another apple 
                                )  
   
            )
Let me know if you use this folks, or if you can find room for improvement. Thanks.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hi mate,

Don't wanna p*ss on your bonfire, but you could have just done this

Code: Select all

echo "<pre>";
print_r($array); 
echo "</pre>";
Gives you nearly the same result.

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

lololol :lol:
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

It was a good idea since it can be hard to assimilate a big, unformatted array.

I use the snippet Bech mentioned as a utility fn - will work on objects as well as arrays.

Code: Select all

<?php
function printR($array)
{
    echo '<pre>';
    print_r($array);
    echo '</pre>';
}
?>
Last edited by McGruff on Tue Aug 09, 2005 6:41 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Bech100 wrote: Don't wanna p*ss on your bonfire, but you could have just done this

Code: Select all

echo "<pre>";
print_r($array); 
echo "</pre>";
. . . . . . . . . . . . . .oh.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

While we're sharing, this is my array debugging function - I added the ability to pass a bit of text (normally for me it's the array name), the line number and the file that the function is being called from (I always pass __LINE__ and __FILE__). Just makes it easier for me to debug stuff:

Code: Select all

function view_array($array, $text='', $line=0, $file='')
{
	$text = (!empty($text)) ? '<u>'.$text.'</u> -> ' : '';
	
	echo '<pre>';
	echo (!empty($file)) ?  '<b>File:</b> '.$file.'<br />' : '';
	echo (!empty($line)) ?  '<b>Line:</b> '.$line.'<br />' : '';
	echo $text;
	print_r($array);
	echo '</pre>';

} // end func
Mac
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i already posted an Array Debugger here viewtopic.php?t=17770&start=0&postdays= ... highlight=

Might wanna merge that with this thread.

Mark
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I use twigletmac's version, but have added is_array(), is_int() etc. checks, so that I can debug whatever without needing to know what it is I'm sending along...

Just because that I "know" that I'm trying to use an array, doesn't mean that I really am, and because I often wants to debug-print other vars.
Post Reply