这是我在print_r我的数组时得到的。它是一个包含以下值的多维数组。
[7] => Array
(
[0] => 1
[1] => 34
[2] => 181
[3] => 50
)
[9] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 47
)
[2] => Array
(
[0] => 20
[1] => 0
[2] => 1621
[3] => 45
)
[3] => Array
(
[0] => 120
[1] => 0
[2] => 121
[3] => 45
)
我想删除键[1]等于0的所有条目。完成修改后,我的最终数组应该是这样的
[7] => Array
(
[0] => 1
[1] => 34
[2] => 181
[3] => 50
)
[9] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 47
)
有什么想法吗?
答案 0 :(得分:3)
foreach
救援:
foreach($arr as $key => $entry) {
if(isset($entry[1]) && $entry[1] === 0) {
unset($arr[$key]);
}
}
还有一个array_filter
示例:
$arr = array_filter($arr, function($entry) {
return $entry[1] !== 0;
});
(假设至少是php 5.3,尽管你可以通过创建一个命名函数并将其作为第二个参数传递给array_filter
来解决这个问题)
答案 1 :(得分:1)
如果您只想删除值为0的数组,则可以使用array_filter。
<?php
$array = array(1,2,3,4,5,0,'',null);
print_r(array_filter($array));
?>
输出:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
答案 2 :(得分:0)
您可以使用此代码
从数组中删除empty,null和0值代码: -
$array = array('one', 'two', '', 'three', null,'four','0');
$filteredarray = array_values( array_filter($array) );
print_r($filteredarray);
输出: -
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
)
谢谢!!一切都好!!