如何取消设置两个值为零的数组值...
$array1=Array ( [0] => SL [1] => S [2] => M [3] => L [4] => F )
$array2=Array ( [0] => 15 [1] => 22 [2] => 35 [3] => 0 [4] => 0 )
始终第一个数组和第二个数组键相同。我想检查第二个数组值是否为空,如果为空,那么我需要从数组
中删除值我需要输出:
Array ( [0] => SL [1] => S [2] => M )
Array ( [0] => 15 [1] => 22 [2] => 35 )
答案 0 :(得分:0)
假设array1的索引对应于array2的索引,你只需要找到$ array2的索引,其值为0.一旦你完成了,只需从两个数组中取消设置给定索引的元素
foreach($array2 as $key=>$value){
if($value === 0){
unset($array2[$key]);
unset($array1[$key]);
}
}
答案 1 :(得分:0)
function removeZeroValues($array) {
foreach ($array as $key => $value) {
if ($value === 0) {
unset($array[$key]);
}
}
return $array;
}
$array1 = array("SL", "S", "M", "L", "F");
$array2 = array(15, 22, 35, 0, 0);
$array1 = removeZeroValues($array1);
$array2 = removeZeroValues($array2);
echo "<pre>";
print_r($array1);
print_r($array2);
答案 2 :(得分:0)
您可以使用array_filter,
$array2 = array_filter($array2);
$array1 = array_filter($array1, function($k)use($array2){return isset($array2[$k]);}, ARRAY_FILTER_USE_KEY);