php 在php中检查数组是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8068220/
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
Check If array is null or not in php
提问by Manish Jangir
I have an array like below which is generated by parsing a xml url.
我有一个如下所示的数组,它是通过解析 xml url 生成的。
The array is
该数组是
Array
(
[Tags] => SimpleXMLElement Object
(
[0] =>
)
)
The array name is $result
. Now I want to check that if the array received like above I want to print a message of failure. But how to check this array in if condition?
数组名称是$result
. 现在我想检查数组是否像上面一样接收到我想打印一条失败消息。但是如何在 if 条件下检查这个数组?
回答by Martin Samson
you can use
您可以使用
empty($result)
to check if the main array is empty or not.
检查主数组是否为空。
But since you have a SimpleXMLElement object, you need to query the object if it is empty or not. See http://www.php.net/manual/en/simplexmlelement.count.php
但是由于您有一个 SimpleXMLElement 对象,您需要查询该对象是否为空。见http://www.php.net/manual/en/simplexmlelement.count.php
ex:
前任:
if (empty($result) || !isset($result['Tags'])) {
return false;
}
if ( !($result['Tags'] instanceof SimpleXMLElement)) {
return false;
}
return ($result['Tags']->count());
回答by Webbu
Corrected;
更正;
/*
return true if the array is not empty
return false if it is empty
*/
function is_array_empty($arr){
if(is_array($arr)){
foreach($arr as $key => $value){
if(!empty($value) || $value != NULL || $value != ""){
return true;
break;//stop the process we have seen that at least 1 of the array has value so its not empty
}
}
return false;
}
}
回答by Anish Rai
This checks if the array is empty
这将检查数组是否为空
if (!empty($result) {
// do stuf if array is not empty
} else {
// do stuf if array is empty
}
This checks if the array is null or not
这将检查数组是否为空
if (is_null($result) {
// do stuf if array is null
} else {
// do stuf if array is not null
}
回答by Yuriy Gergilenko
Right code of two ppl before ^_^
^_^ 前两个人的正确代码
/* return true if values of array are empty
*/
function is_array_empty($arr){
if(is_array($arr)){
foreach($arr as $value){
if(!empty($value)){
return false;
}
}
}
return true;
}
回答by shajji
if array is look like this [null] or [null, null] or [null, null, null, ...]
如果数组看起来像这样 [null] 或 [null, null] 或 [null, null, null, ...]
you can use implode:
你可以使用内爆:
implode is use for convert array to string.
implode 用于将数组转换为字符串。
if(implode(null,$arr)==null){
//$arr is empty
}else{
//$arr has some value rather than null
}
回答by Leysam Rosario
I understand what you want. You want to check every data of the array if all of it is empty or at least 1 is not empty
我明白你想要什么。您想检查数组的每个数据是否全部为空或至少有 1 个不为空
Empty array
空数组
Array ( [Tags] => SimpleXMLElement Object ( [0] => ) )
数组 ( [Tags] => SimpleXMLElement 对象 ( [0] => ) )
Not an Empty array
不是空数组
Array ( [Tags] => SimpleXMLElement Object ( [0] =>,[1] => "s" ) )
数组 ( [Tags] => SimpleXMLElement 对象 ( [0] =>,[1] => "s" ) )
I hope I am right. You can use this function to check every data of an array if at least 1 of them has a value.
我希望我是对的。您可以使用此函数检查数组中的每个数据,如果其中至少有 1 个具有值。
/*
return true if the array is not empty
return false if it is empty
*/
function is_array_empty($arr){
if(is_array($arr)){
foreach($arr $key => $value){
if(!empty($value) || $value != NULL || $value != ""){
return true;
break;//stop the process we have seen that at least 1 of the array has value so its not empty
}
}
return false;
}
}
if(is_array_empty($result['Tags'])){
//array is not empty
}else{
//array is empty
}
Hope that helps.
希望有帮助。