-2

我在打印数据时遇到问题。在我的程序中,我得到对象中的数据并放入一个数组中。说 array1[$id] = 数据对象。整个 array1 然后将被放入 array2['list'] 像 array2['list'] = array1;

问题是我如何获取 id、name 和 description 等数据。这是整个数组的 print_r:

这实际上是数组的结果,我不知道如何访问它。我想获取并获取名称并打印它们:

Array ( 
[list] => Array ( 
    [0] => Array ( 
        [0] => stdClass Object ( 
            [id] => 1 
            [name] => harry potter 4 
            [description] => harry potter and the goblet of fire book by j.k. rowling 
            [unit_price] => 300.99 
        ) 
    ) 
    [1] => Array (
        [0] => stdClass Object ( 
            [id] => 4
            [name] => lipton tea
            [description] => yellow label tea
            [unit_price] => 15.00 
        ) 
    ) 
    [2] => Array (
        [0] => stdClass Object (
            [id] => 9
            [name] => tisyu
            [description] => tissue twenty pieces
            [unit_price] => 20.00
        ) 
    )

) 

)
4

1 回答 1

1

您必须通过以下方式访问它们:

foreach($array['list'] as $array_item){
    $object = $array_item[0];

    echo $object->id."<br />";
    echo $object->name."<br />";
    echo $object->description."<br />";
    echo $object->unit_price."<br />";
}

这将产生:

1
harry potter 4
harry potter and the goblet of fire book by j.k. rowling
300.99
4
lipton tea
yellow label tea
15.00
9
tisyu
tussue twenty pieces
20.00

您可以使用 -> 运算符访问对象属性,后跟要访问的属性。

于 2012-10-02T05:34:33.957 回答