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.

1

3 Answers 3

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';
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Cast it to array and check :

$array = (array) $object;

if (empty($array))

or

count($array)

Comments

0

It can be done by

$array = array_filter($array);
if(!empty($array)) {
    echo "not empty";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.