PHP循环遍历嵌套数组以查找特定值,然后获取该数组中获取匹配项的所有元素

PHP循环遍历嵌套数组以查找特定值,然后获取该数组中获取匹配项的所有元素

问题描述:

I have the following multi dimensional array:

Array
(
[0] => Array
    (
        [name] => Botline
        [description] => Kwese
        [parent_team_id] => 
        [company_id] => 10554
        [id] => 13482
        [external_id] => 13482
        [extras] => 
    )

[1] => Array
    (
        [name] => Books Boutique
        [description] => Kwese
        [parent_team_id] => 
        [company_id] => 10554
        [id] => 13483
        [external_id] => 13483
        [extras] => 
    )

[2] => Array
    (
        [name] => Sij Investments
        [description] => Kwese
        [parent_team_id] => 
        [company_id] => 10554
        [id] => 13484
        [external_id] => 13484
        [extras] => 
    )

[3] => Array
    (
        [name] => Steamcard Investments
        [description] => Kwese installations
        [parent_team_id] => 
        [company_id] => 10554
        [id] => 13549
        [external_id] => 13549
        [extras] => 
    )
)

I want to be able to loop through the array searching for a nested array which has a 'id' equal to lest say '13484'. After I find that 'id' I want to be able to get all the elements in that array and save them.

The number of nested arrays which might come in the main array are undefined so I can not create a predetermined way of getting the right array which has the matching value. How can I achieve this?

我有以下多维数组: p>

  Array \  n(
 [0] =>数组
(
 [名称] => Botline 
 [描述] => Kwese 
 [parent_team_id] => 
 [company_id] => 10554 \  n [id] => 13482 
 [external_id] => 13482 
 [extras] => 
)
 
 [1] =>数组
(
 [名称] =>  Books Boutique 
 [描述] => Kwese 
 [parent_team_id] => 
 [company_id] => 10554 
 [id] => 13483 
 [external_id] => 13483 
 [extras  ] => 
)
 
 [2] =>数组
(
 [名称] => Sij Investments 
 [描述] => Kwese 
 [parent_team_id] => 
  [company_id] => 10554 
 [id] => 13484 
 [external_id] => 13484 
 [extras] => 
)
 
 [3] =>数组
(  
 [name] => Steamcard Investments 
 [description] => Kwese instal  lations 
 [parent_team_id] =>  
 [company_id] =>  10554 
 [id] =>  13549 
 [external_id] =>  13549 
 [extras] =>  
)
)
  code>  pre> 
 
 

我希望能够在数组中循环搜索一个嵌套数组,该数组的“id”等于“13484” ”。 在我找到'id'后,我希望能够获取该数组中的所有元素并保存它们。 p>

主数组中可能出现的嵌套数组的数量是未定义的,所以 我无法创建一个获得具有匹配值的正确数组的预定方法。 我怎样才能做到这一点? p> div>

No need to loop, just re-index using the id and then reference that element. This assumes that id is unique:

$array  = array_column($array, null, 'id');
$result = $array[13484];

Or if you only need it once:

$result = array_column($array, null, 'id')[13484];

Assuming you name your multidimensional array as $someArrays and use $savedArray for saving the found array

$savedArray = array();

foreach ($someArrays as $someArray) {
    if ($someArray['id'] == '13549') {
        $savedArray = $someArray;
        break;
    }
}

print_r($savedArray);

Here you can the according to the id and it's related values

$array = [[

        'name' => 'Botline',
        'description' => 'Kwese',
        'parent_team_id' => '',
        'company_id' => '10554',
        'id' => '13482',
        'external_id' => '13482',
        'extras' => ''
]];

$id = [];

foreach ($array as $key => $value) {
    $id[$value['id']]=$value;
}

after assign to id here, the output of id as below

echo "<pre>";
print_r($id);
echo "</pre>";


Array
(
    [13482] => Array
        (
            [name] => Botline
            [description] => Kwese
            [parent_team_id] => 
            [company_id] => 10554
            [id] => 13482
            [external_id] => 13482
            [extras] => 
        )

)
echo "<pre>";
print_r($id['13482']['name']);
echo "</pre>";