3v4l.org

run code in 300+ PHP versions simultaneously
<?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
Output for 5.6.38 - 5.6.40, 7.0.33, 7.1.0 - 7.1.30, 7.2.0 - 7.2.33, 7.3.0 - 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.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
/Bond1/name/first_name - James /Bond1/name/last_name - Bond /Bond1/aliases/0 - 007 /Bond1/aliases/1 - Bond /Bond1/profiles/0/0 - unknown /Bond1/profiles/1 - 007 /Bond1/profiles/2/2 - secret agent ------- ------- ------- /Bond4/name/first_name - James /Bond4/name/last_name - Bond /Bond4/aliases/0 - 007 /Bond4/aliases/1 - Bond /Bond4/profiles/0/0 - unknown /Bond4/profiles/1 - 007 /Bond4/profiles/2/2 - secret agent ------- /Bond5/name/first_name - James /Bond5/name/last_name - Bond /Bond5/aliases/0 - 007 /Bond5/aliases/1 - Bond /Bond5/profiles/0/0 - unknown /Bond5/profiles/1 - 007 /Bond5/profiles/2/2 - secret agent
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
194.28 ms | 408 KiB | 5 Q