I got below set of empty object array and when I am using php empty
function, below object array bypass the empty
function.
stdClass Object
(
)
Please suggest me, how can I identify the blank 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.
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.
$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).
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";
}