note 87192 added to function.print-r
| From: | everett at myersnetwork dot com | Date: | Sun, 23 Nov 2008 23:02:25 +0000 |
| Subject: | note 87192 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146849@lists.php.net to get a copy of this message | ||
I like the print_r() function as I am sure many of you do, however when I am working on a script via
a browser the output it not easy to read so I have created my own variation hope this helps any of
you guys who have had the same frustration.
Start File ###########################################
<?php
/*
File Name: print_ar.php
PHP: 5.2.3
*/
function print_ar($array, $count=0) {
$i=0;
$tab ='';
while($i != $count) {
$i++;
$tab .= " | ";
}
foreach($array as $key=>$value){
if(is_array($value)){
echo $tab."[<strong><u>$key</u></strong>]<br />";
$count++;
print_ar($value, $count);
$count--;
}
else{
$tab2 = substr($tab, 0, -12);
echo "$tab2~ $key: <strong>$value</strong><br />";
}
$k++;
}
$count--;
}
?>
End File ###########################################
Use is exactly like the print_r() function example;
<?php
require("print_ar.php");
$array = array(
"Main1" => array(
"Sub1" => "Data 1-1",
"Sub2" => "Data 1-2",
"Sub3" => "Data 1-3",
),
"Main2" =>
array (
"Sub1" => "Data 2-1",
"Sub2" => "Data 2-2",
"Sub3" => "Data 2-3",
)
);
print_ar($array);
?>
Will give you:
[Main1]
|~ Sub1: Data 1-1
|~ Sub2: Data 1-2
|~ Sub3: Data 1-3
[Main2]
|~ Sub1: Data 2-1
|~ Sub2: Data 2-2
|~ Sub3: Data 2-3
Vice:
Array ( [Main1] => Array ( [Sub1] => Data 1-1 [Sub2] => Data 1-2 [Sub3] => Data 1-3 )
[Main2] => Array ( [Sub1] => Data 2-1 [Sub2] => Data 2-2 [Sub3] => Data 2-3 ) )
----
Server IP: 69.147.83.197
Probable Submitter: 74.182.105.166
----
Manual Page -- http://www.php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/87192
Del: integrated -- https://master.php.net/note/delete/87192/integrated
Del: useless -- https://master.php.net/note/delete/87192/useless
Del: bad code -- https://master.php.net/note/delete/87192/bad+code
Del: spam -- https://master.php.net/note/delete/87192/spam
Del: non-english -- https://master.php.net/note/delete/87192/non-english
Del: in docs -- https://master.php.net/note/delete/87192/in+docs
Del: other reasons-- https://master.php.net/note/delete/87192
Reject -- https://master.php.net/note/reject/87192
Search -- https://master.php.net/manage/user-notes.php