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?

1cor22
  • 1
  • 2
  • 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. – Don't Panic Jul 05 '18 at 15:34
  • Thank you for checking this out @Don'tPanic. I have updated my question to hopefully clarify. – 1cor22 Jul 05 '18 at 18:14
  • You may be able to use the 'treewalker' library to help you here ----> https://github.com/lukascivil/TreeWalker – CSSBurner Jul 05 '18 at 18:23

1 Answers1

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"
  }
}
lovelace
  • 1,195
  • 1
  • 7
  • 10
  • 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? – Fixxer Dec 11 '19 at 15:17
  • Not sure this is the sort of thing you're after, but maybe something like [this](https://3v4l.org/NSgKQ)... - 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? – lovelace Dec 11 '19 at 17:13