How to check empty 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';
}
?>
How to check if stdClass object is empty or not in php?
Cast to an array first
$tmp = (array) $object;
var_dump(empty($tmp));
The reason is, that an object is an object and there is no useful definition of "an empty object", because there are enough classes out there, that only contains methods, but no properties. Should they considered as "empty"?
Check if object is empty or has values
You should be using OR
, not AND
to combine the conditions. And count()
can never return less than 0
; if the object has no properties, the count will be 0
.
if(!is_object($user) || count(get_object_vars($user)) == 0) {
$this->client->addClient(array("login" => $user_data[0]->username, "password" => $password));
}
How to check if JSON object is empty in PHP?
How many objects are you unserializing? Unless empty(get_object_vars($object))
or casting to array proves to be a major slowdown/bottleneck, I wouldn't worry about it – Greg's solution is just fine.
I'd suggest using the the $associative
flag when decoding the JSON data, though:
json_decode($data, true)
This decodes JSON objects as plain old PHP arrays instead of as stdClass
objects. Then you can check for empty objects using empty()
and create objects of a user-defined class instead of using stdClass
, which is probably a good idea in the long run.
test object is null in php
Use the function is_null()
as follows :
is_null($listcontact);
The Return Value is :
Returns TRUE if var is null, FALSE otherwise.
EDIT
Also you can use this :
if ( !$YOUR_OBJECT->count() ){
//null
}
For more information see those answers
Try using array_filter()
$EmptyArray= array_filter($listcontact);
if (!empty($EmptyArray)){
}
else{
//nothing there
}
Is it right to use empty() function for checking an object is null?
php has the function is_null()
to determine whether an object is null or not: http://php.net/manual/en/function.is-null.php
Check if any property in a object is empty
You can iterate over $this
and throw your exception on first empty property found:
public function __construct()
{
$this->api_url = env('SUPRE_API');
$this->token = env('SUPRE_TOKEN');
foreach ($this as $key => $value) {
if ($value == null) {
throw new \Exception("Could not find {$key} value. Are you sure it has been set?");
}
}
}
Related Topics
Reverse the Letters in Each Word of a String
Running Composer Returns: "Could Not Open Input File: Composer.Phar"
Using Usort in PHP with a Class Private Function
How to Preview an Image Before and After Upload
Convert Jpg/Gif Image to Png in PHP
What Are .Tpl Files? PHP, Web Design
Redirecting to Authentication Dialog - "An Error Occurred. Please Try Again Later"
Apache Mod Rewrite for Laravel
How to Get the Newest File in a Directory in PHP
How to Change PHP's Eregi to Preg_Match
MySQL - Access Denied for User
Prevent Nginx 504 Gateway Timeout Using PHP Set_Time_Limit()
Phpexcel Auto Size Column Width
Using the PHP Http_Accept_Language Server Variable
Test If Port Open and Forwarded Using PHP
Php, Get Tomorrows Date from Date