如何找到在PHP中没有空值,如果条件
问题描述:
在这里,我有一个数组中的一个数组我想找到在前端处不空数据并显示,此数组中假定所有键值空显示意味着我要显示所有值都为空,假设关键中的任何一个不为空意味着我要显示什么是价值如何找到在PHP中没有空值,如果条件
<?php
$array = array('a' => '','b' => 'Kani' , 'c' => '', 'd' => 'Raja');
if (in_array(null, $array)) {
echo "There are null values.";
}else{
echo "Not Null";
}
?>
这里关键的a和d不为空,所以我想取这个关键值如Kani 和Raja
答
如果你想不空不空数组尝试像这样在这里
<?php
print_r(array_filter(array('a' => '','b' => 'Kani' , 'c' => '', 'd' => 'Raja')));
?>
答
嘿,你可以使用这个:
$notNulvals = array();
$index =0;
foreach ($array as $key => $value) {
if ($value) {
array_push($notNulvals, $value);
$index=1;
}
}
if ($index!=0) {
echo "all values are null";
} else {
echo $notNulvals; //you can display it the way you want
}
答
我忘了指定的第三个参数in_array。
if (in_array(null, $array, true)) {
echo "There are null values.";
}else{
echo "Not Null";
}
这样它会告诉数组中是否存在实际的空值。
你们的榜样数组不包含任何'null'值,但一些空字符串。 – insertusernamehere
可以请你更新你的答案 –