【问题标题】:php echo nested object [duplicate]php echo嵌套对象[重复]
【发布时间】:2012-11-11 12:04:41
【问题描述】:

可能重复:
Able to see a variable in print_r()'s output, but not sure how to access it in code

我想“回显”更多嵌套的对象。 我看到了几个关于这个问题的帖子 - 但这让我感到害怕。

我有一个名为“arrResult”的数组/对象,而 print_r(arrResult) 输出是:

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
                        )

                )

        )

)

我想回显所有 [pos]。 'echo arrResult[result][0][pos][0]->pos_txt', 'echo arrResult[result][0][pos][1]->pos_txt',...

我想过这个:

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

我的大脑不明白。 有人可以帮我弄这个吗?

【问题讨论】:

    标签: php arrays object foreach nested


    【解决方案1】:

    你可以使用

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

    输出

    Paketname
    Paketname2
    

    See Live Demo

    【讨论】:

    • 这正是我所需要的,而且有效!谢谢!
    【解决方案2】:

    使用 xdebug 扩展,无论如何它都是你开发时必须拥有的东西,它稍微改变了 var_dump 的工作方式,我相信它会解决你的问题

    see xdebug documentation

    【讨论】:

      【解决方案3】:
      $num = count($arrResult['result']);
      for ($i=0; $i <= $num; $i++) {
          echo $arrResult['result'][$i]->pos[0]->pos_txt;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        • 1970-01-01
        • 2018-07-19
        • 2020-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多