如果我希望代码看起来像这样
,有没有办法foreach ($a as $key=>$value) {
if (( $value['someprop'] == 'some prop' ) || ( $value['someprop'] == 'some other prop' ))
array_splice($a, $key, 1);
因为数组键是用array_splice移位的
答案 0 :(得分:1)
使用for循环,从数组的末尾开始倒数:
(这样你就不会在索引改变时遇到麻烦)
for($i = count($myArray); $i>0; $i--)
{
$value = $myArray[$i];
if (( $value['someprop'] == 'some prop' ) || ( $value['someprop'] == 'some other prop' ))
array_splice($myArray, $i-1, 1);
}
答案 1 :(得分:1)
使用更优雅的array_filter
代替:
$a = [
['someprop' => 'some prop'],
['someprop' => 'not some prop'],
['someprop' => 'some other prop'],
];
方法:
$filtered = array_filter($a, function($value) {
return !($value['someprop'] == 'some prop' || $value['someprop'] == 'some other prop');
});
的print_r($滤波的):
Array
(
[1] => Array
(
[someprop] => not some prop
)
)