php stdClass 检查属性是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20385664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php stdClass check for property exist
提问by june8
I have read some similar issues here but unfortunately find the solution for my case.
我在这里阅读了一些类似的问题,但不幸的是找到了适合我的案例的解决方案。
Some part of the output for an API connection is as;
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 )
Some strings do not include gpps property.
某些字符串不包含 gpps 属性。
What I have done is as follows
我所做的如下
foreach($sales->saleItems->saleItem as $sale)
{
$status = $sale->reviewState;
if(property_exists($sale, gpps))
{
$subId = $sale->gpps->gpp[0]->_;
}else{
$subId = "0-0";
}
}
What I want is I the gpps property is not included in that string $subId stored as 0-0 in db, otherwise get the data from the string. But it doesn't get the strings without gpps.
我想要的是 gpps 属性不包含在该字符串 $subId 中,存储为 0-0 在 db 中,否则从字符串中获取数据。但是没有gpps就不会得到字符串。
Where is my mistake?
我的错误在哪里?
回答by Matteo Tassinari
Change
改变
if(property_exists($sale, gpps))
with
和
if(property_exists($sale, "gpps"))
notice how now gppsis passed as string, as per the specs of the property_existsfunction:
注意 nowgpps是如何作为字符串传递的,根据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()returnsTRUEeven if the property has the valueNULL.
bool property_exists ( mixed $class , string $property )此函数检查给定的属性是否存在于指定的类中。
注意:与 相反
isset(),即使属性具有值,也会property_exists()返回。TRUENULL
回答by JC.
property_existsis the method designed for this purpose.
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.
bool property_exists ( mixed $class , string $property )
这个函数检查给定的属性是否存在于指定的类中。
注意:与 isset() 不同,即使属性值为 NULL,property_exists() 也会返回 TRUE。
回答by Qullbrune
Try a simple hack and use count, since the property contains an array, and I guess, that count(array) == 0 is the same case as when the property is not set.
尝试一个简单的 hack 并使用 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";
}
}
Sure, this is not the most beautiful solution, but since the php-function do not work as expected, I thought a bit more pragmatic.
当然,这不是最漂亮的解决方案,但由于 php-function 没有按预期工作,我认为更务实一点。
回答by Isthatso
Another way , get_object_vars
另一种方式,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";
}
}
}

![php 通过 post [文件上传] 将表单数据提交到 iframe](/res/img/loading.gif)