访问数组和对象 [英] accessing array and objects

查看:122
本文介绍了访问数组和对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的打印数据的问题。在我的计划,我得到这是在对象放在一个阵列中的数据。说数组1 [$ ID] =数据对象。整个数组1,然后将被投入到ARRAY2 ['名单']像数组2 ['名单'] = ARRAY1;

问题是如何获得的数据,如ID,名称和说明。这里是整个阵列的print_r:

这实际上是数组的结果,我不知道如何访问此。我想的foreach并获得名字,并打印出来:

 阵列(
[名单] =>阵列(
    [0] =>阵列(
        [0] => stdClass的对象(
            [ID] => 1
            [名] =>哈利·波特4
            [说明] =>哈利·波特与火的书由J.K.高脚杯罗琳
            [UNIT_PRICE] => 300.99
        )
    )
    [1] =>阵列(
        [0] => stdClass的对象(
            [ID] => 4
            [名] =>立顿红茶
            [说明] =>黄色标签茶
            [UNIT_PRICE] => 15.00
        )
    )
    [2] =>阵列(
        [0] => stdClass的对象(
            [ID] => 9
            [名] => tisyu
            [说明] =>组织二十几块
            [UNIT_PRICE] => 20.00
        )
    )))


解决方案

您将不得不访问它们类似如下:

 的foreach($数组['名单']为$ array_item){
    $对象= $ array_item [0];    回声$对象 - > ID< BR />中。
    回声对象 - $>的名字。< BR />中;
    回声$对象 - >的说明。< BR />中;
    回声$对象 - > UNIT_PRICE。< BR />中;
}

这将产生:

  1
哈利·波特4
哈利·波特与火的书由J.K.高脚杯罗琳
300.99
4
立顿红茶
黄色标签茶
15.00
9
tisyu
tussue二十几块
20.00

您可以通过访问一个对象的属性 - >运营商,其次是你要访问的属性。

I have a problem with printing my data. In my program, i get the data which was in object and put in an array. say array1[$id] = dataobject. the whole array1 then will be put to array2['list'] like array2['list'] = array1;

the problem is how do i get the data such as the id, name and description.. here's the print_r of the whole array:

this is actually the result of the array, i am not sure how to access this. i want to foreach and get the name and print them:

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
        ) 
    )

) 

)

解决方案

You would have to access them something like the following:

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 />";
}

This would yield:

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

You can access an objects properties using the -> operator, followed by the property you wish to access.

这篇关于访问数组和对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆