php echo嵌套对象

时间:2012-11-11 12:04:41

标签: php arrays object foreach nested

  

可能重复:
  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;
    }
}

我的大脑没有得到它。 有人可以帮我弄这个吗?

3 个答案:

答案 0 :(得分:2)

您可以使用

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

答案 1 :(得分:0)

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

see xdebug documentation

答案 2 :(得分:0)

$num = count($arrResult['result']);
for ($i=0; $i <= $num; $i++) {
    echo $arrResult['result'][$i]->pos[0]->pos_txt;
}