dqyy38265 2012-11-11 12:04
浏览 21
已采纳

php echo嵌套对象[复制]

Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code

I want to 'echo' a little bit more nested object. I saw several posts to this question - but this is freaking me out.

I got an Array/Object named 'arrResult' and the print_r(arrResult) output is:

Array
(
    [status] => stdClass Object
        (
            [code] => 0
            [message] => Success
        )

    [result] => Array
        (
            [0] => stdClass Object
                (
                    [base] => stdClass Object
                        (
                            [id] => 3
                            [created] => 2012-11-11 12:11:07
                            [start] => 2012-11-11
                        )

                    [pos] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 4
                                    [invoices_id] => 3
                                    [article_id] => 1
                                    [quantity] => 1
                                    [unit] => Monate
                                    [pos_txt] => Paketname
                                )

                        )

                    [summ] => stdClass Object
                        (
                            [net] => 2.52
                            [discount] => 0
                            [tax] => 0.47899159663865
                            [gross] => 3
                            [rounded_net] => 2.52
                        )

                )

            [1] => stdClass Object
                (
                    [base] => stdClass Object
                        (
                            [id] => 2
                            [created] => 2012-11-11 12:10:39
                            [start] => 2012-11-11
                        )

                    [pos] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 3
                                    [invoices_id] => 2
                                    [article_id] => 2
                                    [quantity] => 1
                                    [unit] => Monate
                                    [pos_txt] => Paketname2
                                )

                        )

                    [summ] => stdClass Object
                        (
                            [net] => 5.04
                            [discount] => 0
                            [tax] => 0.95798319327731
                            [gross] => 6
                            [rounded_net] => 5.04
                        )

                )

        )

)

I want to echo all [pos]. Something like 'echo arrResult[result][0][pos][0]->pos_txt', 'echo arrResult[result][0][pos][1]->pos_txt',...

I thought about this:

foreach ((array)$arrResult['result'] as $key => $contract) {
    foreach ($contract as $key => $objPos){
        echo $objPos->pos_txt;
    }
}

My brain doesn't get it. Can someone help me with this?

  • 写回答

3条回答 默认 最新

  • duan198299 2012-11-11 12:12
    关注

    You can use

    echo "<pre>";
    foreach(array_map(function($v){ return $v->pos ;}, $data['result']) as $list)
    {
        foreach($list as $objPos)
            echo $objPos->pos_txt,PHP_EOL;
    }
    

    Output

    Paketname
    Paketname2
    

    See Live Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题