1

I'm looking for a solution to get an array from a multidimensional array

For example; This is my array:

Array
(
    [1] => Array
        (
            [id] => 2
            [name] => Laïla Mertens 
            [text] => Laïla Mertens 
            [parent_id] => 
            [href] => http://google.com
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Erwin Dubois
                            [text] => Erwin Dubois
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => Test Gebruiker
                                            [text] => Test Gebruiker
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                    [1] => Array
                                        (
                                            [id] => 4
                                            [name] => Catharina
                                            [text] => Catharina
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 5
                            [name] => Araxanta
                            [text] => Araxanta
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 15
                                            [name] => Kevin
                                            [text] => Kevin
                                            [parent_id] => 5
                                            [href] => http://google.com
                                            [nodes] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 16
                                                            [name] => Petra
                                                            [text] => Petra
                                                            [parent_id] => 15
                                                            [href] => http://google.com
                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 17
                                                            [name] => Shannah
                                                            [text] => Shannah
                                                            [parent_id] => 15
                                                            [href] => http://google.com
                                                        )

                                                )

                                        )

                                )

                        )

                    [2] => Array
                        (
                            [id] => 6
                            [name] => Christian
                            [text] => Christian
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 13
                                            [name] => Macy VanMaele
                                            [text] => Macy VanMaele
                                            [parent_id] => 6
                                            [href] => http://google.com
                                        )

                                    [1] => Array
                                        (
                                            [id] => 14
                                            [name] => April Poizat
                                            [text] => April Poizat
                                            [parent_id] => 6
                                            [href] => http://google.com
                                            [nodes] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 7
                                                            [name] => Sigrid
                                                            [text] => Sigrid
                                                            [parent_id] => 14
                                                            [href] => http://google.com
                                                        )

                                                )

                                        )

                                )

                        )

                    [3] => Array
                        (
                            [id] => 8
                            [name] => Nikita
                            [text] => Nikita
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [4] => Array
                        (
                            [id] => 9
                            [name] => Salina Lemmens
                            [text] => Salina Lemmens
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [5] => Array
                        (
                            [id] => 10
                            [name] => Emmily Polen
                            [text] => Emmily Polen
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [6] => Array
                        (
                            [id] => 11
                            [name] => Jitka Symus
                            [text] => Jitka Symus
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                    [7] => Array
                        (
                            [id] => 12
                            [name] => Amina Gigovic
                            [text] => Amina Gigovic
                            [parent_id] => 2
                            [href] => http://google.com
                        )

                )

        )

)

I'd like to get the array where the id equals 1

So in this example, where [id] => 1 I would like to capture the entire array of id 1, thus:

Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Erwin Dubois
                            [text] => Erwin Dubois
                            [parent_id] => 2
                            [href] => http://google.com
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => Test Gebruiker
                                            [text] => Test Gebruiker
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                    [1] => Array
                                        (
                                            [id] => 4
                                            [name] => Catharina
                                            [text] => Catharina
                                            [parent_id] => 1
                                            [href] => http://google.com
                                        )

                                )

                        )

Is there a way in php where I can have a function like;

function get_subarray($id) {
  // Select array where id = $id
}

Any point towards the direction is greatly appreciated! It would be needed into a function since I would parse different userlevels depending on their unique id

array:

$json = file_get_contents("https://innerbeauthe.be/cp/_data/fetch.php");
$data = json_decode($json,true);
2
  • Is there guaranteed to be only one of any given id? Commented Feb 8, 2021 at 3:19
  • Yes, the id is a unique identifier (PK, UQ) Commented Feb 8, 2021 at 11:11

1 Answer 1

1

You can define a recursive function which traverses the array in search of the desired array key.

Although PHP includes a function array_walk_recursive(), it will traverse into leaf nodes and makes it difficult to return a parent array. Likewise, using RecursiveArrayIterator actually complicates getting at the array you want instead of the leaf notes. Instead it is not too complicated to create your own:

// Function accepts an array and a search value
function walk_for_array(array $a, $search) {
  // Loop over the array
  foreach ($a as $k => $v) {
    // Find sub-arrays (rather than scalar values)
    if (is_array($v)) {
      // Check if there is an 'id' key and if it matches your search value
      if (isset($v['id']) && $v['id'] == $search) {                                                                                           
        // return it
        return $v; 
      }   
      else {
        // Otherwise continue drilling into the array for more arrays
        $sub = walk_for_array($v, $search);
        // Return the recursive value only if the inner call
        // was non-null
        if ($sub) return $sub;
      }   
    }   
  }
}

// Call it, searching for id == 1
$found = walk_for_array($your_big_array, 1);

print_r($found);

// Call it, searching for id == 13
$found = walk_for_array($your_big_array, 13);

print_r($found);

Prints:

Array
(
    [id] => 1
    [name] => Erwin Dubois
    [text] => Erwin Dubois
    [parent_id] => 2
    [href] => http://google.com
    [nodes] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [name] => Test Gebruiker
                    [text] => Test Gebruiker
                    [parent_id] => 1
                    [href] => http://google.com
                )

            [1] => Array
                (
                    [id] => 4
                    [name] => Catharina
                    [text] => Catharina
                    [parent_id] => 1
                    [href] => http://google.com
                )

        )

)

And for id = 13

Array
(
    [id] => 13
    [name] => Macy VanMaele
    [text] => Macy VanMaele
    [parent_id] => 6
    [href] => http://google.com
)
Sign up to request clarification or add additional context in comments.

5 Comments

Hello Michael, Thank you for your response, I tried the function you posted and it worked for the following id's: 1 & 2; If for example, I would select Id = 5, I get no results at all, tried to break down the function in printable arrays but don't see why it doesn't load the id 5 nor the others (3 for example)
I will have some time to look at it in a couple of hours.
@ErwinDubois Okay I think it's working correctly now - the recursive array call was passing through its return even if null. That is fixed to continue searching until the inner call returns a non-null
Thank you! It works like a charm, you saved me hours of head scratching, eye frowning, table-bashing arguements with myselve! haha. Greatly appreciated, also I love the fact that you commented out the function, gives me the time to see the logic in it.
You're welcome. With regard to commenting the function, if someone posts an answer for to your question without good explanation, please don't be afraid to ask them for an explanation with a comment. While I have certainly encountered individuals here who bristle at that, the explanations are much more valuable to future searchers who find the answer.

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.