note 110642 added to function.print-r

From: Date: Fri, 16 Nov 2012 12:52:06 +0000
Subject: note 110642 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-192008@lists.php.net to get a copy of this message
<?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 ?> ---- Server IP: 69.147.83.199 Probable Submitter: 213.177.96.108 ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/110642 Del: integrated -- https://master.php.net/note/delete/110642/integrated Del: useless -- https://master.php.net/note/delete/110642/useless Del: bad code -- https://master.php.net/note/delete/110642/bad+code Del: spam -- https://master.php.net/note/delete/110642/spam Del: non-english -- https://master.php.net/note/delete/110642/non-english Del: in docs -- https://master.php.net/note/delete/110642/in+docs Del: other reasons-- https://master.php.net/note/delete/110642 Reject -- https://master.php.net/note/reject/110642 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#192008) next »