2013-02-25 104 views
0

这里的价值是与obj:我如何获得一个OBJ标签

array(2) { 
    [0]=> object(stdClass)#538 (9) 
    { 
     ["term_id"]=> string(3) "152" 
     ["name"]=> string(19) "Éducation physique" 
     ["slug"]=> string(18) "education-physique" 
     ["term_group"]=> string(1) "0" 
     ["term_taxonomy_id"]=> string(3) "159" 
     ["taxonomy"]=> string(11) "product_cat" 
     ["description"]=> string(0) "" 
     ["parent"]=> string(3) "123" 
     ["count"]=> string(1) "3" 
    } 
    [1]=> object(stdClass)#540 (9) 
    { 
     ["term_id"]=> string(3) "123" 
     ["name"]=> string(5) "Sport" 
     ["slug"]=> string(5) "sport" 
     ["term_group"]=> string(1) "0" 
     ["term_taxonomy_id"]=> string(3) "123" 
     ["taxonomy"]=> string(11) "product_cat" 
     ["description"]=> string(0) "" 
     ["parent"]=> string(1) "0" 
     ["count"]=> string(2) "49" 
    } 
} 
mam : 

我试图获得的价值:[term_id] 152我需要它为“152”的值在变量。我尝试:$product_category->term_id它返回“一无所有” 和我尝试:$product_category['term_id']它返回“一无所有”

如何是“正确”的方式提前从对象retreive值

的感谢!

回答

1

如果我正在读你,而$product_category是整个变量,你有一个数组中的两个对象。所以你需要告诉PHP你试图访问对象之前的数组项。

$product_category[0]->term_id应该工作。

+0

孩子,我看起来很愚蠢,当它全部在一行时就很难看清......你是绝对正确的......两个对象,我必须选择一个......我的错! – menardmam 2013-02-25 21:12:08

+0

非常好 - 很高兴帮助 – Hobo 2013-02-25 21:20:05

0

下面是另一种解决方案:

转换这个目的是利用json_decode阵列,然后从阵列得到的值

$array = json_decode($json_string, true); 

$term_id = $array[0]['term_id']; 

OR获取这两个值:

foreach($array as $val){ 
    echo $val['term_id']; 
}