I have an array
我有一个阵列
$array = array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null);
i would like to determine if all the array keys have empty values if so then return false. the above example should return false as it does not have any value. but if one or more keys have any values then it should return true for example the below example is true.
我想确定所有数组键是否具有空值,如果是,则返回false。上面的例子应该返回false,因为它没有任何值。但是如果一个或多个键具有任何值,则它应该返回true,例如下面的示例为true。
$array = array('key1', 'key2' => value2, 'key3', 'key4' => value4);
6 个解决方案
#1
41
Assuming you actually mean an array like
假设你实际上是指一个类似的数组
array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null)
the answer is simply
答案很简单
if (!array_filter($array)) {
// all values are empty (where "empty" means == false)
}
#2
4
Your assumption is incorrect. array('key1', 'key2', 'key3', 'key4')
has 4 values and keys in the range 0..3
.
你的假设不正确。 array('key1','key2','key3','key4')有4个值,键在0..3范围内。
array('key1', 'key2' => value2, 'key3', 'key4' => value4)
has the value key1
(with key 0), the key key2
, the value key3
(with key 1) and the key key4
.
array('key1','key2'=> value2,'key3','key4'=> value4)的值为key1(带键0),键key2,值key3(带键1)和键key4 。
#3
2
@Blagovest is correct about your incorrect question presentation.
@Blagovest对您不正确的问题陈述是正确的。
$allEmpty = true;
foreach( $array as $key => $val ) {
if( isset( $array[$key] ) ) {
$allEmpty = false;
break;
}
}
// Do what you will with $allEmpty
#4
1
I think what you mean is to check whether all keys are numeric or if at least one is string:
我想你的意思是检查所有键是否都是数字或者至少有一个是字符串:
$ok = false;
foreach( array_keys($array) as $key ){
if(is_string($key)){
$ok=true;
break;
}
}
return $ok;
#5
0
$flag = 0;
foreach($array as $keys)
{
if(!isempty($keys)) {
$flag++;
}
}
if(flag > 0)
{
echo "Array not empty!";
}
else {
echo "Array empty!";
}
Should work.
应该管用。
#6
0
$array = array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null);
The answer is
答案是
$filterArray = array_filter($array);
if(count($filterArray) == 0){
return false;
}else{
return true;
}
#1
41
Assuming you actually mean an array like
假设你实际上是指一个类似的数组
array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null)
the answer is simply
答案很简单
if (!array_filter($array)) {
// all values are empty (where "empty" means == false)
}
#2
4
Your assumption is incorrect. array('key1', 'key2', 'key3', 'key4')
has 4 values and keys in the range 0..3
.
你的假设不正确。 array('key1','key2','key3','key4')有4个值,键在0..3范围内。
array('key1', 'key2' => value2, 'key3', 'key4' => value4)
has the value key1
(with key 0), the key key2
, the value key3
(with key 1) and the key key4
.
array('key1','key2'=> value2,'key3','key4'=> value4)的值为key1(带键0),键key2,值key3(带键1)和键key4 。
#3
2
@Blagovest is correct about your incorrect question presentation.
@Blagovest对您不正确的问题陈述是正确的。
$allEmpty = true;
foreach( $array as $key => $val ) {
if( isset( $array[$key] ) ) {
$allEmpty = false;
break;
}
}
// Do what you will with $allEmpty
#4
1
I think what you mean is to check whether all keys are numeric or if at least one is string:
我想你的意思是检查所有键是否都是数字或者至少有一个是字符串:
$ok = false;
foreach( array_keys($array) as $key ){
if(is_string($key)){
$ok=true;
break;
}
}
return $ok;
#5
0
$flag = 0;
foreach($array as $keys)
{
if(!isempty($keys)) {
$flag++;
}
}
if(flag > 0)
{
echo "Array not empty!";
}
else {
echo "Array empty!";
}
Should work.
应该管用。
#6
0
$array = array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null);
The answer is
答案是
$filterArray = array_filter($array);
if(count($filterArray) == 0){
return false;
}else{
return true;
}