Array Traversal

In PHP it's easy to get JSON decoded by $data = json_decode($input). But many newcomers are confused by how to pick individual information from the resulting array or object structure then. This visualization is intended to help.

Hover with the mouse over entries or keys to see the required array or object traversal syntax:

$data Your JSON might be corrupt. See JSONLint. Test if you need to disable magic_quotes or something.

"; } #-- print if ($data) { print_r_js($data); } #-- foreach if (@($_REQUEST["foreach"])) { print "

How to loop over this structure

\n"; print foreach_r_js($data); print "

Note that this auto-generated loop might only work if the array/object structure is very uniform (no discrepancies in subarray schemes). It also might skip consecutive indexed array levels.

"; } #-- copy link print "


link"; ?>

Also check the manual on array syntax and how to work with objects.
Or how to use a foreach to loop over entries.

Simply paste in your JSON blob or print_r output to get a visualization.

The print_r parsing failed.

It's only an approximation, and 100% correctly transforming it back into an array is near impossible. Check that it was indeed in print_r format, not var_dump, or xdebug output. Otherwise gimme the original JSON data blob.

"); } return $data; } /** * CSSified output of an array. * */ function print_r_js($data, $path='$data', $level=1) { switch ($type = gettype($data)) { case "array": print "

Array(
\n"; foreach ($data as $key => $value) { $_key = key_array($key); $_key_nq = key_array($key, NULL); print "$_key_nq => \n"; print_r_js($value, "{$path}{$_key}", $level+1); } print "
)
\n"; break; case "object": print "
stdClass Object(
\n"; foreach ($data as $key => $value) { $_key = key_object($key); $_key_ar = key_array($key, NULL); $_key_nq = key_object($key, NULL); print "$_key_ar => \n"; print_r_js($value, "{$path}->{$_key}", $level+1); } print "
)
\n"; break; default: $data = h($data); print "$data
\n"; break; } } /** * Look for numeric entries and generate some loop examples. * */ function foreach_r_js($data, $path='$data', $_valuekey='', $skip=0, $pad="", $i="i") { if (is_array($data) or is_object($data)) { foreach ($data as $key=>$value) { // traverse if (!is_int($key) or $skip) { if (is_array($data)) { $_key = key_array($key); } else { $_key = "->" . key_object($key); } return foreach_r_js($value, "$path$_key", "$_valuekey$_key", 0, $pad, $i); } // print foreach else { return "
{$pad}foreach ($path as \$$i=>\$value) {\n"
                    . foreach_r_js($value, $path."[\$$i]", $_valuekey='', $skip=FALSE, "$pad    ", ++$i)
                    . "{$pad}}
"; } } } // print loop innards else { return "{$pad} print \$value$_valuekey;\n"; } } /** * html escape */ function h($str) { return htmlspecialchars($str, ENT_QUOTES, "UTF-8"); } function key_array($key, $q="'") { return h( is_numeric($key) ? "[$key]" : "[$q".addcslashes("$key", '\'\\')."$q]" ); } function key_object($key) { return h(preg_match("/^[a-z_]\w*$/i", $key) ? $key : "{'".addcslashes($key, '\'\\')."'}"); } ?>