duanjumie8753
duanjumie8753
2017-10-12 17:43

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duan0802 duan0802 4年前

    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];
    
    点赞 评论 复制链接分享
  • dtncv04228 dtncv04228 4年前

    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>";
    
    点赞 评论 复制链接分享
  • dongtu4559 dongtu4559 4年前

    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);
    
    点赞 评论 复制链接分享

相关推荐