note 81479 added to function.print-r

From: Date: Fri, 29 Feb 2008 18:28:10 +0000
Subject: note 81479 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-136609@lists.php.net to get a copy of this message
A slight modification to the previous post to allow for arrays containing mutli line strings. haven't fully tested it with everything, but seems to work great for the stuff i've done so far. <?php function print_r_reverse(&$output) { $expecting = 0; // 0=nothing in particular, 1=array open paren '(', 2=array element or close paren ')' $lines = explode("\n", $output); $result = null; $topArray = null; $arrayStack = array(); $matches = null; while (!empty($lines) && $result === null) { $line = array_shift($lines); $trim = trim($line); if ($trim == 'Array') { if ($expecting == 0) { $topArray = array(); $expecting = 1; } else { trigger_error("Unknown array."); } } else if ($expecting == 1 && $trim == '(') { $expecting = 2; } else if ($expecting == 2 && preg_match('/^\[(.+?)\] \=\> (.+)$/', $trim, $matches)) // array element { list ($fullMatch, $key, $element) = $matches; if (trim($element) == 'Array') { $topArray[$key] = array(); $newTopArray =& $topArray[$key]; $arrayStack[] =& $topArray; $topArray =& $newTopArray; $expecting = 1; } else { $topArray[$key] = $element; } } else if ($expecting == 2 && $trim == ')') // end current array { if (empty($arrayStack)) { $result = $topArray; } else // pop into parent array { // safe array pop $keys = array_keys($arrayStack); $lastKey = array_pop($keys); $temp =& $arrayStack[$lastKey]; unset($arrayStack[$lastKey]); $topArray =& $temp; } } // Added this to allow for multi line strings. else if (!empty($trim) && $expecting == 2) { // Expecting close parent or element, but got just a string $topArray[$key] .= "\n".$line; } else if (!empty($trim)) { $result = $line; } } $output = implode(n, $lines); return $result; } /** * @param string $output : The output of a multiple print_r calls, separated by newlines * @return mixed[] : parseable elements of $output */ function print_r_reverse_multiple($output) { $result = array(); while (($reverse = print_r_reverse($output)) !== NULL) { $result[] = $reverse; } return $result; } $output = ' Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z [3] => Array ( [nest] => yes [nest2] => Array ( [nest] => some more asffjaskkd ) [nest3] => o rly? ) ) ) some extra stuff '; var_dump(print_r_reverse($output), $output); ?> This should output array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> array(4) { [0]=> string(1) "x" [1]=> string(1) "y" [2]=> string(1) "z" [3]=> array(3) { ["nest"]=> string(3) "yes" ["nest2"]=> array(1) { ["nest"]=> string(40) "some more asffjaskkd" } ["nest3"]=> string(6) "o rly?" } } } string(18) "nsome extra stuffn" Added: else if (!empty($trim) && $expecting == 2) { // Expecting close parent or element, but got just a string $topArray[$key] .= "\n".$line; } ---- Server IP: 64.71.164.2 Probable Submitter: 208.67.191.194 ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/81479 Del: integrated -- https://master.php.net/note/delete/81479/integrated Del: useless -- https://master.php.net/note/delete/81479/useless Del: bad code -- https://master.php.net/note/delete/81479/bad+code Del: spam -- https://master.php.net/note/delete/81479/spam Del: non-english -- https://master.php.net/note/delete/81479/non-english Del: in docs -- https://master.php.net/note/delete/81479/in+docs Del: other reasons-- https://master.php.net/note/delete/81479 Reject -- https://master.php.net/note/reject/81479 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136609) next »