note 118071 added to function.print-r
| From: | joseantonio at splitfy dot com | Date: | Tue, 29 Sep 2015 09:01:40 +0000 |
| Subject: | note 118071 added to function.print-r | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203872@lists.php.net to get a copy of this message | ||
This is an update from Matt's print_r_reverse() solution that works not only with Arrays, but
also with stdClass objects.
I expect someone finds it useful.
<?php
function print_r_reverse($in) {
$lines = explode("\n", trim($in));
if ( (trim($lines[0]) != 'Array') && (trim($lines[0]) != 'stdClass
Object') ){
// bottomed out to something that isn't an array or object
return $in;
} else {
// this is an array or stdClass object, lets parse it
if (preg_match("/(\s{4,})\(/", $lines[1], $match)) {
// this is a tested array/recursive call to this function
// take a set of spaces off the beginning
$spaces = $match[1];
$spaces_length = strlen($spaces);
$lines_total = count($lines);
for ($i = 0; $i < $lines_total; $i++) {
if (substr($lines[$i], 0, $spaces_length) == $spaces) {
$lines[$i] = substr($lines[$i], $spaces_length);
}
}
}
$class = trim( $lines[0] );
array_shift($lines); // Array or stdClass Object
array_shift($lines); // (
array_pop($lines); // )
$in = implode("\n", $lines);
// make sure we only match stuff with 4 preceding spaces (stuff for this array and not a
nested one)
preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE |
PREG_SET_ORDER);
$pos = array();
$previous_key = '';
$in_length = strlen($in);
// store the following in $pos:
// array with key = key of the parsed array's item
// value = array(start position in $in, $end position in $in)
foreach ($matches as $match) {
$key = $match[1][0];
$start = $match[0][1] + strlen($match[0][0]);
$pos[$key] = array($start, $in_length);
if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
$previous_key = $key;
}
// behave differently depending on the type
switch( $class ){
case 'Array':
$ret = array();
foreach ($pos as $key => $where) {
// recursively see if the parsed out value is an array/object too
$ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
}
break;
case 'stdClass Object':
$ret = new stdClass();
foreach ($pos as $key => $where) {
// recursively see if the parsed out value is an array/object too
$ret->$key = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
}
break;
default:
throw new Exception( 'Type not allowed' );
}
return $ret;
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 84.127.239.199
----
Manual Page -- http://php.net/manual/en/function.print-r.php
Edit -- https://master.php.net/note/edit/118071
Del: integrated -- https://master.php.net/note/delete/118071/integrated
Del: useless -- https://master.php.net/note/delete/118071/useless
Del: bad code -- https://master.php.net/note/delete/118071/bad+code
Del: spam -- https://master.php.net/note/delete/118071/spam
Del: non-english -- https://master.php.net/note/delete/118071/non-english
Del: in docs -- https://master.php.net/note/delete/118071/in+docs
Del: other reasons-- https://master.php.net/note/delete/118071
Reject -- https://master.php.net/note/reject/118071
Search -- https://master.php.net/manage/user-notes.php