0

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?

Community
  • 1
  • 1
Sven OS
  • 35
  • 1
  • 5

3 Answers3

2

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

Baba
  • 94,024
  • 28
  • 166
  • 217
0

Use xdebug extension, it's something what you must have for development anyway, it slightly changes how var_dump works and I believe it will solve your problem

see xdebug documentation

Zdenek Machek
  • 1,758
  • 1
  • 20
  • 30
0
$num = count($arrResult['result']);
for ($i=0; $i <= $num; $i++) {
    echo $arrResult['result'][$i]->pos[0]->pos_txt;
}
Smi
  • 13,850
  • 9
  • 56
  • 64