多维数组从子数组中删除项目

multidimensional array remove item from child array

我的多维数组有问题。我想通过 $id 值从子数组中删除一些项目。

这是我的多维示例数组和 selectedIds:

$myArray = [
    ['id' => '2', 
    'name' => 'Punk'
    ],[
    'id' => '5', 
    'name' => 'Rock', 
    'children' => [
        '30' => ['id' => '30', 
                'name' => 'Hard Rock', 
                'parentId' => '5'
                ], 
        '40' => ['id' => '40', 
                'name' => 'Soft Rock', 
                'parentId' => '5'
                ],  
        '50' => ['id' => '50', 
                'name' => 'Glam Rock', 
                'parentId' => '5'
                ]
        ]
    ]
];

$selectedIds = [2,5,30];

我想从数组中删除那些不在 selectedIds 数组中的项目。

所以我想要输出:

$outputArray = [
    [
    'id' => '2', 
    'name' => 'Punk'
    ],[
    'id' => '5', 
    'name' => 'Rock', 
    'children' => [
            '30' => ['id' => '30', 
                    'name' => 'Hard Rock', 
                    'parentId' => '5']
                ]
    ]
];

我尝试用 foreach 和 array_key_exist 实现它,但它不正确:

foreach ($myArray as $key=>$value) {
    if (array_key_exists('children', $value)) {
        foreach ($selectedIds as $id) {
            if (isset($value['children'][$id])) {
                $outputArray[] = $value['children'][$id];
            }
        }
    }
}
print_r($outputArray);

此输出只是 ID 为 30 的项目

如果数组没有 children key

,请将 else part 写入您的代码
else{
    foreach ($selectedIds as $id) {
        if ($value['id'] == $id) {
            $outputArray[$key] = $value;
        }
    }
}

最终代码

<?PHP
$myArray = [
    [
        'id' => '2', 
        'name' => 'Punk'
    ],
    [
        'id' => '5', 
        'name' => 'Rock', 
        'children' => [
            '30' => ['id' => '30', 
                    'name' => 'Hard Rock', 
                    'parentId' => '5'
                    ], 
            '40' => ['id' => '40', 
                    'name' => 'Soft Rock', 
                    'parentId' => '5'
                    ],  
            '50' => ['id' => '50', 
                    'name' => 'Glam Rock', 
                    'parentId' => '5'
                    ]
            ]
        ]
];

$selectedIds = [2,5,30];

foreach ($myArray as $key=>$value) {
    if (array_key_exists('children', $value)) {
        foreach ($selectedIds as $id) {
            if (isset($value['children'][$id])) {
                $outputArray[$key]['id'] = $value['id'];
                $outputArray[$key]['name'] = $value['name'];
                $outputArray[$key]['children'][$id] = $value['children'][$id];
            }
        }
    }else{
        foreach ($selectedIds as $id) {
            if ($value['id'] == $id) {
                $outputArray[$key] = $value;
            }
        }
    }
}
echo "<pre>";
print_r($outputArray);
?>

输出

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Punk
        )

    [1] => Array
        (
            [id] => 5
            [name] => Rock
            [children] => Array
                (
                    [30] => Array
                        (
                            [id] => 30
                            [name] => Hard Rock
                            [parentId] => 5
                        )

                )

        )

)

嘿,我让其他代码更简单了。就在这里:

稍微改变一下 foreach 的内容:

foreach ($myArray as $key=>&$value) {
    if (array_key_exists('children', $value)) {
        $value['children'] = array_intersect_key($value['children'], array_flip($selectedIds));
    }
}

使用 array_flip 转换值上的键,使用 array_intersect_key 仅获取存在于 2 个数组中的 ID。

在这里你可以看到一个live demo

注意:注意我在 foreach 中通过 referer &$value 传递值以修改原始数组,如果您不想这样做,只需复制原始数组并修改第二个。