0

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.

1 Answer 1

2

Try this instead:

function removeKeyFromArray(&$array, $key_to_remove) 
{ 
  foreach ($array as $key => &$value) 
  { 
    if ($key==$key_to_remove) 
    { 
        unset($array[$key]);
    } 
    elseif (is_array($value))
    {
        removeKeyFromArray($value, $key_to_remove); 
    } 
  }
}

The way you did it would never find the keys to remove because they are arrays too so would go through the first part of the if/else statement all the time, hence never being unset.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Small change with big effect :) Your Answer will be accepted as soon as I am able to - 5 more minutes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.