<?php
function pathify($array, $path, &$output = []){ // declare $output as an empty array by default and modify by reference
foreach($array as $k => $v){
if (is_array($v)) { // if can recurse
pathify($v, "$path/$k", $output); // append key to path, access $v array
}else{
$output[] = "$path/$k - $v"; // push completed string to output array
}
}
return $output; // return array of path strings
}
$json='{
"name":{
"first_name":"James",
"last_name":"Bond"
},
"aliases":["007","Bond"],
"profiles":[{"0":"unknown"},"007",{"2":"secret agent"}]
}';
$data = json_decode($json, true); // decode json to an array
echo implode("\n", pathify($data, "/Bond1")); // separate the returned string with a newline
echo "\n-------\n";
echo implode("\n", pathify([], "/Bond2")); // separate the returned string with a newline
echo "\n-------\n";
echo implode("\n", pathify([], "/Bond3")); // separate the returned string with a newline
echo "\n-------\n";
echo implode("\n", pathify($data, "/Bond4")); // separate the returned string with a newline
echo "\n-------\n";
echo implode("\n", pathify($data, "/Bond5")); // separate the returned string with a newline