foreach ($arr as $key => $value) {
foreach ($arr_ as $key_ => $value_) {
$dev[$value_][] = array_search($value_, $arr[$key]);
}
}
我需要从数组搜索中排除所有布尔值false,但保留原始键。 所以,这就是我想要的AAAA。我怎么能这样做?
'AAAA' =>
array
1 => int 0
3 => int 1
5 => int 1
7 => int 2
9 => int 1
11 => int 2
13 => int 2
15 => int 3
我有什么:
array
'AAAA' =>
array
0 => boolean false
1 => int 0
2 => boolean false
3 => int 1
4 => boolean false
5 => int 1
6 => boolean false
7 => int 2
8 => boolean false
9 => int 1
10 => boolean false
11 => int 2
12 => boolean false
13 => int 2
14 => boolean false
15 => int 3
'BBBB' =>
array
0 => boolean false
1 => boolean false
2 => int 0
3 => int 0
4 => boolean false
5 => boolean false
6 => int 1
7 => int 1
8 => boolean false
9 => boolean false
10 => int 1
11 => int 1
12 => boolean false
13 => boolean false
14 => int 2
15 => int 2
'CCCC' =>
array
0 => boolean false
....
答案 0 :(得分:3)
对于单个阵列,您可以使用它:
foreach($array as $key => $element) {
if($element === false) {
unset($array[$key]);
}
}
然后将其包装在另一个foreach循环中以处理其他循环(请注意引用&$array
):
foreach($arrays as &$array) {
foreach($array as $key => $element) {
if($element === false) {
unset($array[$key]);
}
}
}
答案 1 :(得分:1)
$arr = array(false,0,false,1,false,2,false,0,false);
$out = array_filter($arr, function($in) { return false !== $in; });
var_dump($out);
输出:
array(4) {
[1]=>
int(0)
[3]=>
int(1)
[5]=>
int(2)
[7]=>
int(0)
}
答案 2 :(得分:0)
请在此处查看此答案:Delete an element from an array
您可以使用unset()来保存由if语句(if false) { unset($array[$index]) } etc
触发的键。
答案 3 :(得分:0)
使用array_filter
,它会保存原始密钥php.net: array_filter
$newArr=array_filter($oldArr, function($el) { return $el !== false; })