3

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.

Pupil
  • 23,834
  • 6
  • 44
  • 66
Er Sharad Soni
  • 300
  • 5
  • 16
  • 4
    Possible duplicate of [How to check that an object is empty in PHP?](http://stackoverflow.com/questions/9412126/how-to-check-that-an-object-is-empty-in-php) – Narendrasingh Sisodia Oct 26 '15 at 09:53

3 Answers3

2

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';
}
?>
Pupil
  • 23,834
  • 6
  • 44
  • 66
1

Cast it to array and check :

$array = (array) $object;

if (empty($array))

or

count($array)
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

It can be done by

$array = array_filter($array);
if(!empty($array)) {
    echo "not empty";
}
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
varad mayee
  • 619
  • 7
  • 19