0

I have a deep nested multidimensional array. I'm wanting to be able to search for multiple key/value pairs and return it's parent matches.

Example array:

//$array
Array
(
[0] => Array
        [ID] => 1
        [Name] => My Name
        [GroupLocations] => Array
            (
                [0] => Array
                    (

                        [GroupId] => 111
                        [LocationId] => 222 
                        [Location] => Array
                            (
                                [Name] => My Location
                                [Street1] => 555 Somewhere Lane
                                [City] => City
                                [County] => County
                                [State] => CA
                                [Country] => US
                                [PostalCode] => 00000
                            )
                    )

            )
    )
[1] => Array
        [ID] => 2
        [Name] => My Other Name
        [GroupLocations] => Array
            (
                [0] => Array
                    (

                        [GroupId] => 222
                        [LocationId] => 333 
                        [Location] => Array
                            (
                                [Name] => My Other Location
                                [Street1] => 666 Other Rd.
                                [City] => City
                                [County] => County
                                [State] => CA
                                [Country] => US
                                [PostalCode] => 00000
                            )
                    )

            )
    )
)

Something similar to PHP Search an Array for multiple key / value pairs but accounting for the deeper levels.

So if I have the example array above and want to filter the array and get results based off of filters that need to search different nested parts of the array

$filters = array(
  'ID' => $_GET[‘ID’],
  'LocationId' => $_GET['LocationId']
);

$results = filterMyArray($array, $filters);

Where filterMyArray function would recursively filter all levels with the array of filters and return all matching indexes/. Struggling to come up with a decent solution. Any suggestions on how to accomplish this?

3
  • 1
    Your description is a bit vague. Adding some examples of expected output of various inputs would really help to clarify what you're trying to get. Commented Jul 5, 2018 at 15:34
  • Thank you for checking this out @Don'tPanic. I have updated my question to hopefully clarify. Commented Jul 5, 2018 at 18:14
  • You may be able to use the 'treewalker' library to help you here ----> github.com/lukascivil/TreeWalker Commented Jul 5, 2018 at 18:23

1 Answer 1

2

Maybe the following recursive function may help you on your way; you can change the key you're looking for ($key) and it will return all occurances of the key in the nested / multidimensional array.

<?php
$arr = [
    [
        'ID' => 1,
        'Name' => 'My Name',
        'GroupLocations' => [
            [
                'GroupId' => 111,
                'LocationId' => 222,
                'Location' => [
                    'Name' => 'MyLocation',
                    'Street1' => '555 Somewhere Lane'
                ]
            ]
        ]
    ]   
];

$results = []; // the container for the search result(s)
$key = 'Name'; // the key we are looking for
findKey($arr, $key, $results); // invoke the search function

/** print the result of our search */
echo '<pre>';
var_dump($results);
echo '<pre>';

/**
 * @param array $arr the multidimensional array we are searching
 * @param string $key the key we are looking for
 * @param $results           passed by reference - in case the key is found, this array 'stores' the corresponding key-value pair.
 */
function findKey($arr = [], $key = '', &$results = [])
{
    foreach ($arr as $key0 => $value0) {
        if ($key0 == $key && !is_array($value0)) {
            $results[][$key] = $value0;
        }
        if (is_array($value0)) {
            findKey($value0, $key, $results);
        }
    }
    return false;
}

Output:

array(2) {
  [0]=>
  array(1) {
    ["Name"]=>
    string(7) "My Name"
  }
  [1]=>
  array(1) {
    ["Name"]=>
    string(10) "MyLocation"
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @lovelace I've been looking for something like this! Can you explain how I would return the value of the searched key? e.g. return "My Name" so that I can assign it to a variable, rather that the array numbers and keys?
Not sure this is the sort of thing you're after, but maybe something like this... - new code between /* NEW */ and /* END NEW */. The thing is you can have multiple 'hits' for the same key (e.g. "Name") in the nested arrays, so you need the $results array to store them. You can then iterate over the results array to assign found values to newly created variables using $$ notation. Hope that helps?

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.