PHP搜索深层嵌套数组以获取多个键/值组合

时间:2018-07-05 15:30:35

标签: php

我有一个深层嵌套的多维数组。我希望能够搜索多个键/值对并返回其父项匹配项。

示例数组:

//$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
                            )
                    )

            )
    )
)

类似于PHP Search an Array for multiple key / value pairs的东西,但是占了更深层次。

因此,如果我有上面的示例数组,并且想要过滤该数组并从需要搜索该数组不同嵌套部分的过滤器中获取结果

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

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

在哪里filterMyArray函数将使用过滤器数组递归过滤所有级别并返回所有匹配的index /。努力提出一个体面的解决方案。有关如何实现此目标的任何建议?

1 个答案:

答案 0 :(得分:1)

也许以下递归函数可能对您有所帮助;您可以更改要查找的键($ key),它将返回嵌套/多维数组中所有出现的键。

<?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;
}

输出:

array(2) {
  [0]=>
  array(1) {
    ["Name"]=>
    string(7) "My Name"
  }
  [1]=>
  array(1) {
    ["Name"]=>
    string(10) "MyLocation"
  }
}