note 81238 added to function.print-r

From: Date: Tue, 19 Feb 2008 20:34:51 +0000
Subject: note 81238 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-136284@lists.php.net to get a copy of this message
This is a better print_r reverse algorithm, that works with arbitrary nested arrays. Anything else it treats as strings. The second function allows you to take a string with multiple print_r results concatenated, and returns the result of parsing each of them. <?php /** * @param string &$output : The output of a print_r call; this parameter is DESTRUCTIVE, and will be set to the remainder * of $output which is not parsed. * @return mixed : the first parseable element of $output */ 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; } } 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 ) [nest3] => o rly? ) ) ) some extra stuff '; var_dump(print_r_reverse($output), $output); ?> The above example will 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(9) "some more" } ["nest3"]=> string(6) "o rly?" } } } string(20) " some extra stuff " ---- Server IP: 64.71.164.2 Probable Submitter: 128.62.151.166 ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/81238 Del: integrated -- https://master.php.net/note/delete/81238/integrated Del: useless -- https://master.php.net/note/delete/81238/useless Del: bad code -- https://master.php.net/note/delete/81238/bad+code Del: spam -- https://master.php.net/note/delete/81238/spam Del: non-english -- https://master.php.net/note/delete/81238/non-english Del: in docs -- https://master.php.net/note/delete/81238/in+docs Del: other reasons-- https://master.php.net/note/delete/81238 Reject -- https://master.php.net/note/reject/81238 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136284) next »