-1

i have a multi dimensional array with data structure like below,how to iterate to get the desired out put

Array
(
    [P1-VVS2-VVS2] => Array
        (
            [S] => Array
                (
                    [1] => Array
                        (
                            [key] => P1-VVS2-VVS2
                            [saw] => S
                            [row_num] => 1
                            [assign] => 0
                            [total_dollar] => 1490140.75
                            [stone_weight] => 24.5
                            [dollar] => 4963.00
                        )

                )

            [C] => Array
                (
                    [1] => Array
                        (
                            [key] => P1-VVS2-VVS2
                            [saw] => C
                            [row_num] => 1
                            [assign] => 0
                            [total_dollar] => 6080976
                            [stone_weight] => 44
                            [dollar] => 6282.00
                        )

                )

        )

)

the desired out put like, how can be use the for each loop or any other to display the desired result data

    P1-VVS2-VVS2
    S
    1
    0
    1490140.75
    24.5
    4963.00


    P1-VVS2-VVS2
    C
    1
    0
    6080976
    44
    6282.00
LF00
  • 27,015
  • 29
  • 156
  • 295
Muhammad ali
  • 287
  • 4
  • 17
  • 2
    what you have tried? – B. Desai Jun 15 '17 at 08:41
  • Please try [https://stackoverflow.com/questions/4222983/simplify-a-nested-array-into-a-single-level-array](https://stackoverflow.com/questions/4222983/simplify-a-nested-array-into-a-single-level-array) – Binary Brackets Jun 15 '17 at 08:41
  • *"how can be use the for each loop"* -- have you tried anything? Take a look at some examples in the [documentation of `foreach`](http://php.net/manual/en/control-structures.foreach.php). – axiac Jun 15 '17 at 08:51
  • Refer to this [post](https://stackoverflow.com/q/44537419/6521116) – LF00 Jun 15 '17 at 08:51

4 Answers4

2
foreach($array as $key=>$item){
    echo $key."<br>";
    foreach($item as $subKey=>$subItem){
        echo $subKey."<br>";;
        foreach($subItem as $value){
            echo $value."<br>";
        }
    }
}
muttalebm
  • 552
  • 1
  • 6
  • 22
1

You can use array_walk_recursive, if you also want to output the middle space refer to this post for extendd array_walk_recursive.

array_walk_recursive($array, function($v){
  echo $v . "\n";

});
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    A link to the documentation of [`array_walk_recursive()`](http://php.net/manual/en/function.array-walk-recursive.php) is worth one thousand words (and an upvote :-)). – axiac Jun 15 '17 at 08:53
1

You can try this

foreach ($array as $arr){

    array_walk_recursive($arr, function($res){
        echo $res . "\n". '<br>';
    });
}
Farhad Hossen
  • 263
  • 1
  • 6
0
<?php

$marray = array
    (
    'P1 - VVS2 - VVS2' => Array
        (
        'S' => Array
            (
            '1' => Array
                (
                'key' => 'P1 - VVS2 - VVS2',
                'saw' => S,
                'row_num' => 1,
                'assign' => 0,
                'total_dollar' => 1490140.75,
                'stone_weight' => 24.5,
                'dollar' => '4963.00'
            )
        ),
        'C' => Array
            (
            '1' => Array
                (
                'key' => 'P1 - VVS2 - VVS2',
                'saw' => C,
                'row_num' => 1,
                'assign' => 0,
                'total_dollar' => 6080976,
                'stone_weight' => 44,
                'dollar' => '6282.00'
            )
        )
    )
);




foreach ($marray as $key => $item) {
    foreach ($item as $sub_item) {
        foreach ($sub_item as $sub_item1) {
            foreach ($sub_item1 as $sub_item2) {
                print $sub_item2 . '<br>';

            }
            echo '<br><br>';
        }
    }
}

?>
Rajeev Prasad
  • 31
  • 1
  • 3