note 110642 deleted from function.print-r by aharvey
| From: | aharvey@php.net | Date: | Mon, 19 Nov 2012 01:12:29 +0000 |
| Subject: | note 110642 deleted from function.print-r by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192027@lists.php.net to get a copy of this message | ||
Note Submitter: Denis Davydov framelabcms at gmail dot com
----
<?php
$array = array(
'1' => '1',
'2' => array('2.1'),
'3' => array('3.1' => array('3.1.1')),
'4' => array('4.1' => array('4.1.1' =>
array('4.1.1.1'), '4.1.2' => '4.1.2')),
'5' => NULL,
'6' => (int) 6,
'7' => ''
);
?>
Example:
<?php
print_r($array);
?>
Array ( [1] => 1 [2] => Array ( [0] => 2.1 ) [3] => Array ( [3.1] => Array ( [0]
=> 3.1.1 ) ) [4] => Array ( [4.1] => Array ( [4.1.1] => Array ( [0] => 4.1.1.1 )
[4.1.2] => 4.1.2 ) ) [5] => [6] => 6 [7] => )
<?php
function print_r2($data) {
switch(gettype($data)) {
case 'array' :
print ' Array ( ';
break;
case 'object':
print ' stdClass Object ( ';
break;
default:
print $data;
exit;
}
foreach($data as $key => $val) {
print '['.$key . '] => ';
print is_array($val) ? print_r2($val) : $val . ' ';
}
print(' ) ');
}
?>
Example:
<?php
print_r2($array);
?>
Array ( [1] => 1 [2] => Array ( [0] => 2.1 ) [3] => Array ( [3.1] => Array ( [0]
=> 3.1.1 ) ) [4] => Array ( [4.1] => Array ( [4.1.1] => Array ( [0] => 4.1.1.1 )
[4.1.2] => 4.1.2 ) ) [5] => [6] => 6 [7] => )
<?php
print_r2( (object) $array );
?>
stdClass Object ( [1] => 1 [2] => Array ( [0] => 2.1 ) [3] => Array ( [3.1] => Array
( [0] => 3.1.1 ) ) [4] => Array ( [4.1] => Array ( [4.1.1] => Array ( [0] => 4.1.1.1
) [4.1.2] => 4.1.2 ) ) [5] => [6] => 6 [7] => )
<?php
print_r2( 123 ); // 123
print_r('String'); // String
?>
Return types
<?php
function print_r2($data) {
switch($type = gettype($data)) {
case 'array' :
print ' Array ( ';
break;
case 'object':
print ' stdClass Object ( ';
break;
default:
print $type == 'NULL' ? $type : $type . ' ( ' . $data . ' ) ';
exit;
}
foreach($data as $key => $val) {
print '['.$key . '] => ';
print is_array($val) ? print_r2($val) : $val . ' ';
}
print(' ) ');
}
?>
Print "boolean", "integer", "double", "string",
"resource", "NULL", "unknown type"
Example:
<?php
print_r2(1); // integer ( 1 )
print_r2(1.2); // double ( 1.2 )
print_r2('String'); // string ( String )
print_r2(true); // boolean ( 1 )
print_r2(NULL); // NULL
?>