php 如何检查数组值是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9367450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if array value is empty?
提问by Unknown Error
Here is my array ouput
这是我的数组输出
Array
(
[1] => 1
[2] => 2
[3] =>
)
How do I know the [3] =>
is empty?
我怎么知道[3] =>
是空的?
foreach ($array as $key => $value) {
if (empty($value))
echo "$key empty <br/>";
else
echo "$key not empty <br/>";
}
My out put showing all is not empty. What is correct way to check is empty?
我的输出显示所有内容都不是空的。检查是否为空的正确方法是什么?
采纳答案by Martin.
It works as expected, third one is empty
它按预期工作,第三个是空的
Maybe try to trim its value, just in case that third value would be just a space.
也许尝试修剪它的值,以防万一第三个值只是一个空格。
foreach ($array as $key => $value) {
$value = trim($value);
if (empty($value))
echo "$key empty <br/>";
else
echo "$key not empty <br/>";
}
回答by EpokK
An other solution:
另一种解决方案:
$array = array('one', 'two', '');
if(count(array_filter($array)) == count($array)) {
echo 'OK';
} else {
echo 'ERROR';
}
回答by ShoveItUpYour___
You can check for an empty array by using the following:
您可以使用以下命令检查空数组:
if ( !empty(array_filter($array))) {
echo 'OK';
} else {
echo 'EMPTY ARRAY';
}
回答by konsolenfreddy
You can use array_diff()
and array_diff_key()
:
您可以使用array_diff()
和array_diff_key()
:
$array = array('one', 'two', '');
$emptyKeys = array_diff_key(array_diff($array,array()),$array);
array_diff()
extracts all items which are not the same (therefore leaving out the blanks), array_diff_key
gives back the differences to the original array.
array_diff()
提取所有不相同的项目(因此省略空白),array_diff_key
将差异返回给原始数组。
回答by jay thanki
$array = array('A', 'B', '');
or
或者
$array = array('A', 'B', ' ');
An other solution:
另一种解决方案:
this work for me
这对我有用
if(in_array(null, $myArray) || in_array('', array_map('trim',$myArray))) {
echo 'Found a empty value in your array!';
}
回答by Jesus Carrillo
Here is a simple solution to check an array for empty key values and return the key.
这是一个简单的解决方案,用于检查数组中是否存在空键值并返回键。
$a = array('string', '', 5);
echo array_search(null, $a);
// Echos 1
To check if array contains an empty key value. Try this.
检查数组是否包含空键值。尝试这个。
$b = array('string','string','string','string','','string');
if (in_array(null, $b)) {
echo 'We found a empty key value in your array!';
}
回答by coyote34
im using in my project like this for check this array
我在我的项目中使用这样的来检查这个数组
im posting form data like this array('username' => 'john','surname' => 'sins');
我像这样发布表单数据 array('username' => 'john','surname' => 'sins');
public function checkArrayKeyExist($arr) {
foreach ($arr as $key => $value) {
if (!strlen($arr[$key])) {
return false;
}
}
return true;
}
回答by Sharma Vikram
Try this:
尝试这个:
<?php
$data=array(
'title' => 'Test Name Four',
'first_name' => '',
'last_name' => 'M',
'field_company' => 'ABC',
'email' => '',
'client_phone_number' => '',
'address_line_1' => '',
'address_line_2' => 'Address 3',
'address_line_3' => '',
'address_line_4' => '',
'post_code' => '',
);
echo '<pre>';
print_r($data);
foreach ($data as $key => $case ) {
echo "$key => ".is_multiArrayEmpty($case)."<br>";
}
function is_multiArrayEmpty($multiarray) {
if(is_array($multiarray) and !empty($multiarray)){
$tmp = array_shift($multiarray);
if(!is_multiArrayEmpty($multiarray) or !is_multiArrayEmpty($tmp)){
return false;
}
return true;
}
if(empty($multiarray)){
return true;
}
return false;
}
?>