php stdClass 检查属性是否存在

标签 php

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

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/

相关文章:

php - PHP中的自定义排序数组

php - Symfony2 在每个循环中更改元素样式

php - Sentry 2 和 Laravel 4 向用户模型添加字段

php - 以下sql查询中的错误处理

javascript - Wordpress 定制器自定义控件传输 postMessage 不起作用

php - 使用 css 在 vbulletin 中对齐横幅?

javascript - 将逗号分隔列表转换为无序列表

php - 如何将 SELECT ALL 值或 SELECTED 复选框值插入到 mysql 中的不同列中?

php - 为 sql 表中的每一列生成输入提交

php - 如何从mysql到php对月份进行排序