如何正确引用多维数组?

时间:2009-08-30 09:16:25

标签: php arrays

我有以下名为“$ order”的数组(由“print_r”打印出来):

stdClass Object
(
    [products] => Array
    (
        [0] => stdClass Object
        (
             [data] => Array
             (
                 [attributes] => Array
                 (
                     [ID] => Array
                     (
                         [0] => 57
                     )
                  )
             )
         )
    )
)

我的问题是,如何引用“57”?我认为会是这样的:

$order->products[0]->data[attributes][ID][0];

但这不起作用。我错过了什么?

3 个答案:

答案 0 :(得分:2)

$order->products[0]->data['attributes']['ID'][0]

答案 1 :(得分:2)

您缺少数组键的引号。否则,它会让PHP认为attributesID是常量(define('ID', 'foobar'); echo ID;)。

$order->products[0]->data['attributes']['ID'][0];

答案 2 :(得分:0)

没关系。就像这样: $命令 - >产品[0] - >数据[ “属性”] [ “ID”] [0];