我正在尝试使用array_filter函数从数组中删除数组元素。它工作正常,但始终有一个未被删除的元素。
使用一个简单的数组使用array_combine构造该数组,使用array_chunk构造另一个数组。
这是我的代码:
recursiveDelete()
如果出现错误,我不会在这里发问。但是返回没有错误。
制作$arr_a = array("AAA","","CCC","");
$arr_b = array("a1","b1","a2","b2","a3","b3","a4","b4");
$arr_c = array_chunk($arr_b,2);
$c = array_combine($arr_a,$arr_c);
$limpn_url = array_filter($c);
时
输出为:
print_r($limpn_url);
*请注意Array ( [AAA] => Array ( [0] => a1 [1] => b1 ) [] => Array ( [0] => a4 [1] => b4 ) [CCC] => Array ( [0] => a3 [1] => b3 ) )
不应存在。其他空元素已正确删除...
答案 0 :(得分:0)
您可以使用一些数组函数来删除为空的键。
$arr_a = array("AAA","","CCC","");
$arr_b = array("a1","b1","a2","b2","a3","b3","a4","b4");
$arr_c = array_chunk($arr_b,2);
$c = array_combine($arr_a,$arr_c);
$limp= array_filter($c);
$filteredkeys = array_filter(array_keys($limp)); // here i remove the "" key
$filtered = array_intersect_key($limp, array_flip($filteredkeys)); // since $filterdkeys is values i need to flip it and intersect with $limp
var_dump($filtered);
输出:
array(2) {
["AAA"]=>
array(2) {
[0]=>
string(2) "a1"
[1]=>
string(2) "b1"
}
["CCC"]=>
array(2) {
[0]=>
string(2) "a3"
[1]=>
string(2) "b3"
}
}