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?
asked Dec 4, 2013 at 20:51
6
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[]
returnsTRUE
even if the property has the valueNULL
.
answered Dec 4, 2013 at 21:11
Matteo TassinariMatteo Tassinari
17.6k7 gold badges60 silver badges80 bronze badges
property_exists is the method designed for this purpose.
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.
answered Dec 4, 2013 at 21:09
JC.JC.
6703 silver badges12 bronze badges
Try a simple hack and use count, since the property contains an array, and I guess, that count[array] == 0 is the same case as when the property is not set.
foreach[$sales->saleItems->saleItem as $sale]
{
$status = $sale->reviewState;
if[@count[$sale->gpps->gpp] && count[$sale->gpps->gpp] > 0]
{
$subId = $sale->gpps->gpp[0]->_;
}else{
$subId = "0-0";
}
}
Sure, this is not the most beautiful solution, but since the php-function do not work as expected, I thought a bit more pragmatic.
answered Dec 5, 2013 at 11:24
QullbruneQullbrune
1,9252 gold badges20 silver badges20 bronze badges
Another way , get_object_vars
$obj = new stdClass[];
$obj->name = "Nick";
$obj->surname = "Doe";
$obj->age = 20;
$obj->adresse = null;
$ar_properties[]=get_object_vars[$obj];
foreach[$ar_properties as $ar]{
foreach [$ar as $k=>$v]{
if[$k =="surname"]{
echo "Found";
}
}
}
answered Dec 13, 2016 at 23:38
Fevly PallarFevly Pallar
3,0192 gold badges14 silver badges19 bronze badges