我有一个多维,它是一个动态数组,如:
Array
(
[0] => Array
(
[key] => delete
[label] => hi Delete
)
[1] => Array
(
[key] => edit
[label] => hi Edit
)
[2] => Array
(
[key] => update
[label] => hi update
)
)
现在我想从上面的多维数组中删除一个数组:
Array
(
[key] => delete
[label] => hi Delete
)
最后我想要一个输出:
阵 (
[0] => Array
(
[key] => edit
[label] => hi Edit
)
[1] => Array
(
[key] => update
[label] => hi update
)
)
对于我已经尝试过的,下面是我的代码:
<?php
$arr1 = array(array("key" => "delete", "label" => "hi Delete"),array("key" => "edit", "label" => "hi Edit"), array("key" => "update", "label" => "hi update"));
$diff = array_diff_assoc($arr1, array("key" => "delete", "label" => "hi Delete"));
print_r($diff);
?>
但我在输出中得到满$ arr1:
Array
(
[0] => Array
(
[key] => delete
[label] => hi Delete
)
[1] => Array
(
[key] => edit
[label] => hi Edit
)
[2] => Array
(
[key] => update
[label] => hi update
)
)
我该怎么做才能帮助我
答案 0 :(得分:2)
将array_filter与callback
一起使用
$arr1 = array_filter($arr1, function ($var) {
return $var['key'] != 'delete';
});
print_r($arr1);
答案 1 :(得分:1)
你应该循环遍历数组并测试你要删除的键,如下所示:(盲目写,所以你需要测试它!)
<?php
foreach ($arr1 as $thisArrIndex=>$subArray)
{
if ( $subArray['key'] == "delete" )
{
unset($arr1[$thisArrIndex]);
}
}
?>
建议编辑是在找到密钥后突破循环。看起来OP可能有多个像这样的键(在多个子数组中),所以我选择不在这里打破循环。