<?php
function printAll($a, &$output, $prefix = '') {
if (!is_array($a) && !is_object($a)) {
$prefix = rtrim($prefix, '/');
$output[] = $prefix . ' => ' . $a. "\n";
return;
}
foreach($a as $k => $v) {
$tmp = $prefix;
$tmp .= $k . "/";
printAll($v, $output, $tmp);
}
}
$json_str = '{
"one":
{
"two": 3,
"four": [ 5,6,7]
},
"eight":
{
"nine":
{
"ten":11
}
}
}';
$object = json_decode($json_str);
$res = array();
printAll($object, $res);
var_dump($res);
- Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array(5) {
[0]=>
string(13) "one/two => 3
"
[1]=>
string(16) "one/four/0 => 5
"
[2]=>
string(16) "one/four/1 => 6
"
[3]=>
string(16) "one/four/2 => 7
"
[4]=>
string(21) "eight/nine/ten => 11
"
}
preferences:
105.08 ms | 408 KiB | 5 Q