gpt4 book ai didi

php stdClass 检查属性是否存在

转载 作者:IT王子 更新时间:2023-10-29 01:00:12 27 4
gpt4 key购买 nike

我在这里阅读了一些类似的问题,但不幸的是找到了适合我的案例的解决方案。

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() returns TRUE even if the property has the value NULL.

关于php stdClass 检查属性是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com