如何获取值内的stdClass对象的值?

时间:2017-07-07 05:47:01

标签: php json

stdClass Object
(
   [net_type] => Net 1
   [No of Windows] => 2
   [0] => stdClass Object
    (
        [Windows1] => stdClass Object
            (
                [Width1] => 20
                [Height1] => 10
            )

        [Windows2] => stdClass Object
            (
                [Width2] => 40
                [Height2] => 15
            )

    )

   [Pricing/sq ft] => 20
   [Total Area] => 2
   [Total Price] => 5
)

我知道要获得net_type的价值:

$details_decode = json_decode($details);
echo "details==".$details_decode->net_type;

但是如何获得Width1的价值,即:

echo "windows 1==".$details_decode->Windows1['Width1'];

1 个答案:

答案 0 :(得分:1)

您可能最好使用true传递第二个json_decode)参数:

  

当为TRUE时,返回的对象将被转换为关联数组。

即:

$details_decode = json_decode($details, true);

然后像数组一样访问它,这是更好的,因为一些对象属性中有空格。 e.g:

$details_decode['net_type']
$details_decode[0]['Windows1']
$details_decode['Pricing/sq ft']

希望这有帮助。