我在这里阅读了一些类似的问题,但不幸的是找到了适合我的案例的解决方案。
API 连接的部分输出如下;
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
)
)
)
)
有些字符串不包含 gpps 属性。
我的做法如下
foreach($sales->saleItems->saleItem as $sale)
{
$status = $sale->reviewState;
if(property_exists($sale, gpps))
{
$subId = $sale->gpps->gpp[0]->_;
}else{
$subId = "0-0";
}
}
我想要的是 gpps 属性不包含在数据库中存储为 0-0 的字符串 $subId 中,否则从字符串中获取数据。但如果没有 gpps,它就无法获取字符串。
我的错误在哪里?
最佳答案
改变
if(property_exists($sale, gpps))
与
if(property_exists($sale, "gpps"))
注意现在 gpps
是如何按照 property_exists
的签名作为字符串传递的功能:
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
.
关于php stdClass 检查属性是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385664/