[英]How to check empty object array?
I got below set of empty object array and when I am using php empty
function, below object array bypass the empty
function.我得到了一组空对象数组,当我使用 php
empty
函数时,下面的对象数组绕过了empty
函数。
stdClass Object
(
)
Please suggest me, how can I identify the blank object array.请建议我,如何识别空白对象数组。
Convert the object to associative array using json_encode and json_decode .使用json_encode和json_decode将对象转换为关联数组。
$arr = json_decode(json_encode($obj), TRUE);
if (empty($arr)) {
// Object is empty.
}
json_decode
function returns associative array if second parameter is set TRUE
(even if object is passed).如果第二个参数设置为
TRUE
json_decode
函数将返回关联数组(即使传递了对象)。
Working example:工作示例:
<?php
$obj = new stdClass();
$arr = json_decode(json_encode($obj), TRUE);
if (empty($arr)) {
echo 'empty';
}
?>
Cast it to array and check :将其投射到数组并检查:
$array = (array) $object;
if (empty($array))
or或者
count($array)
It can be done by它可以通过
$array = array_filter($array);
if(!empty($array)) {
echo "not empty";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.