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
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue Feb 10, 2004 12:19 pm
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>
//put a couple non-breaking spaces here
//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.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Feb 11, 2004 3:13 am
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
McGruff
DevNet Master
Posts: 2893 Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland
Post
by McGruff » Wed Feb 11, 2004 8:35 am
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.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Feb 11, 2004 9:44 am
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.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Wed Feb 11, 2004 9:50 am
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
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sun Feb 15, 2004 1:42 pm
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.