PHP stdClass检查属性是否存在

54

我在这里阅读了一些类似的问题,但很遗憾没有找到适合我情况的解决方案。

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";
    }
}

我想要的是,如果在数据库中以0-0存储的字符串$subId中不包含gpps属性,则不获取该字符串的数据,否则从字符串中获取数据。但它无法获得不带gpps的字符串。

我的错误在哪里?


请尝试使用isset($sale->gpps) - Iłya Bursov
这意味着你使用错误了,应该使用isset和可能的empty来检查属性被设置但没有元素的情况。 - Iłya Bursov
1
尝试使用property_exists($sale, 'gpps')。 - Qullbrune
@june8 如果 isset 没有起作用,那么你一定是在使用它的时候出了问题。 - cypher
当我使用isset($sale->gpps)或property_exists($sale, 'gpps')时,我只更改了我上面编写的代码的这一部分。我的其余代码有什么问题吗?因为它们都没有起作用。 - june8
property_exists 对我非常有用。谢谢 - Leo Smith
4个回答

108

变化

if(property_exists($sale, gpps)) 

跟随;使用;带有

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

注意现在如何将gpps作为字符串传递,这是根据property_exists函数的签名:

bool property_exists ( mixed $class , string $property )

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

注意:isset()相反,property_exists()即使该属性的值为NULL,也会返回TRUE


2

property_exists是专门设计用来检查属性是否存在的方法。

bool property_exists ( mixed $class , string $property )

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

注意: 与isset()不同,即使属性的值为NULL,property_exists()也返回TRUE。

0
试一下简单的方法,使用count函数。因为该属性包含一个数组,我猜想,当count(array) == 0时,就相当于该属性未设置。
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";
 }
}

当然,这不是最美观的解决方案,但由于php函数未按预期工作,我采用了更加实用主义的思路。


0

另一种方式,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";
    }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接