php stdClass检查属性是否存在

  发布于 2023-02-12 19:14

我在这里已经阅读了一些类似的问题但不幸的是找到了我的案例的解决方案.

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属性不包含在db中存储为0-0的字符串$ subId中,否则从字符串中获取数据.但是如果没有gpps,它就无法获得.

我的错误在哪里?

1 个回答
  • 更改

    if(property_exists($sale, gpps)) 
    

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

    注意现在如何gpps作为字符串传递,根据property_exists函数的规范:

    bool property_exists ( mixed $class , string $property )

    此函数检查指定类中是否存在给定属性.

    注意: 与之相反isset(),即使属性具有值,也会property_exists()返回.TRUENULL

    2023-02-12 19:16 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有