Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php stdClass check for property exist

Tags:

php

I have read some similar issues here but unfortunately find the solution for my case.

Some part of the output for an API connection is as;

stdClass Object (     [page] => 0     [items] => 5     [total] => 5     [saleItems] => stdClass Object (         [saleItem] => Array (             [0] => stdClass Object (                 [reviewState] => approved                 [trackingDate] => 2013-11-04T09:51:13.420+01:00                 [modifiedDate] => 2013-12-03T15:06:39.240+01:00                 [clickDate] => 2013-11-04T09:06:19.403+01:00                 [adspace] => stdClass Object (                     [_] => xxxxx                     [id] => 1849681                  )                 [admedium] => stdClass Object (                     [_] => Version 3                     [id] => 721152                 )                 [program] => stdClass Object (                     [_] => yyyy                     [id] => 10853                 )                 [clickId] => 1832355435760747520                 [clickInId] => 0                 [amount] => 48.31                 [commission] => 7.25                 [currency] => USD                 [gpps] => stdClass Object (                     [gpp] => Array (                         [0] => stdClass Object (                             [_] => 7-75                             [id] => z0                         )                     )                 )                 [trackingCategory] => stdClass Object (                     [_] => rers                     [id] => 68722                 )                 [id] => 86erereress-a9e4-4226-8417-a46b4c9fd5df             )         )     ) ) 

Some strings do not include gpps property.

What I have done is as follows

foreach($sales->saleItems->saleItem as $sale) {     $status     = $sale->reviewState;          if(property_exists($sale, gpps))      {         $subId      = $sale->gpps->gpp[0]->_;     }else{         $subId      = "0-0";     } } 

What I want is I the gpps property is not included in that string $subId stored as 0-0 in db, otherwise get the data from the string. But it doesn't get the strings without gpps.

Where is my mistake?

like image 278
june8 Avatar asked Dec 04 '13 20:12

june8


People also ask

How do you check if a key exists in an object PHP?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

How check stdClass object is empty or not in PHP?

2 answers. You could use the get_object_vars function to return the properties of stdClass to array . Then you could use the function count or empty , to know if it is empty or not.

How can handle undefined property in PHP?

With the help of property_exists() you can easily remove "Undefined property" notice from your php file.


1 Answers

Change

if(property_exists($sale, gpps))  

with

if(property_exists($sale, "gpps")) 

notice how now gpps is passed as string, as per the signature of the property_exists function:

bool property_exists ( mixed $class , string $property )

This function checks if the given property exists in the specified class.

Note: As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

like image 56
Matteo Tassinari Avatar answered Sep 25 '22 08:09

Matteo Tassinari