This is one of these days .. tried a lot of possible solutions and starting to run in circles now. Hope someone can help me.
I retrieve a huge json file and need to remove all nodes (on every level) with a certain key.
I do have an array ($array) similar to this:
Array
(
[result] => Array
(
[0] => Array
(
[title] => my title 1
[subtitle] => my subtitle 1
[pagetree] => Array
(
[0] => Array
(
[uid] => 1016
)
[1] => Array
(
[uid] => 1017
)
[2] => Array
(
[uid] => 4512
)
[3] => Array
(
[uid] => 1018
)
[4] => Array
(
[uid] => 1019
)
[5] => Array
(
[uid] => 1024
)
)
[languageVersions] => Array
(
[de] => Array
(
[pid] => 1016
[title] => Das ist der Titel in DE
[subtitle] => german subtitle
[pagetree] => Array
(
[0] => Array
(
[uid] => 1016
)
[1] => Array
(
[uid] => 1017
)
[2] => Array
(
[uid] => 4512
)
[3] => Array
(
[uid] => 1018
)
[4] => Array
(
[uid] => 1019
)
[5] => Array
(
[uid] => 1024
)
)
[tstamp] => 1410339721
[package] => 1016/course_de
)
[jp] => Array
(
[pid] => 1016
[language_title] => Japanese
[title] => This JAPANESE TITLE
[subtitle] => Japanese SAub Title
[pagetree] => Array
(
[0] => Array
(
[uid] => 1016
)
[1] => Array
(
[uid] => 1017
)
[2] => Array
(
[uid] => 4512
)
[3] => Array
(
[uid] => 1018
)
[4] => Array
(
[uid] => 1019
)
[5] => Array
(
[uid] => 1024
)
)
[tstamp] => 1405960286
[package] => 1016/course_jp
)
[cn] => Array
(
[language_id] => 19
[pid] => 1016
[language_title] => Chinese (simplified)
[title] => Title Chinese
[subtitle] =>
[pagetree] => Array
(
[0] => Array
(
[uid] => 1016
)
[1] => Array
(
[uid] => 1017
)
[2] => Array
(
[uid] => 4512
)
[3] => Array
(
[uid] => 1018
)
[4] => Array
(
[uid] => 1019
)
[5] => Array
(
[uid] => 1024
)
)
[tstamp] => 1404520858
[package] => 1016/course_cn
)
[th] => Array
(
[language_id] => 29
[pid] => 1016
[language_short] => th
[language_title] => Thai
[title] => thai title
[subtitle] =>
[tstamp] => 1414136060
[package] => 1016/course_th
)
)
)
)
[1] => Array
(
[uid] => 1657
[pid] => 2
[language_id] => 0
[language_short] => default
[language_title] => English
[title] => Dive Guide
[subtitle] =>
[pagetree] => Array
(
[0] => Array
(
....
I want to remove all Nodes on all levels with key [pagetree].
I found several posts here in StackOverflow regarding this problem, but could not manage to get them working (Delete element from multi-dimensional array based on key, Recursive search and remove in array?)
My Approach is:
function removeKeyFromArray(&$array, $key_to_remove)
{
foreach ($array as $key => &$value)
{
if (is_array($value))
{
removeKeyFromArray($value, $key_to_remove);
}
elseif ($key==$key_to_remove)
{
unset($array[$key]);
}
}
}
$json = file_get_contents('http://url.with.json.file');
$data = json_decode($json);
$array = objectToArray($data);
removeKeyFromArray($array, 'pagetree');
echo "<pre>";
print_r($array);
echo "</pre>";
After the cleanup, the array should look like;
Array
(
[result] => Array
(
[0] => Array
(
[title] => my title 1
[subtitle] => my subtitle 1
[languageVersions] => Array
(
[de] => Array
(
[pid] => 1016
[title] => Das ist der Titel in DE
[subtitle] => german subtitle
[tstamp] => 1410339721
[package] => 1016/course_de
)
[jp] => Array
(
[pid] => 1016
[language_title] => Japanese
[title] => This JAPANESE TITLE
[subtitle] => Japanese SAub Title
[tstamp] => 1405960286
[package] => 1016/course_jp
)
[cn] => Array
(
[language_id] => 19
[pid] => 1016
[language_title] => Chinese (simplified)
[title] => Title Chinese
[subtitle] =>
[tstamp] => 1404520858
[package] => 1016/course_cn
)
[th] => Array
(
[language_id] => 29
[pid] => 1016
[language_short] => th
[language_title] => Thai
[title] => thai title
[subtitle] =>
[tstamp] => 1414136060
[package] => 1016/course_th
)
)
)
)
[1] => Array
(
[uid] => 1657
[pid] => 2
[language_id] => 0
[language_short] => default
[language_title] => English
[title] => Dive Guide
[subtitle] =>
....
Thanks for your assistance.