-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
4

4 Answers 4

2
foreach($array as $key=>$item){
    echo $key."<br>";
    foreach($item as $subKey=>$subItem){
        echo $subKey."<br>";;
        foreach($subItem as $value){
            echo $value."<br>";
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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";

});

1 Comment

A link to the documentation of array_walk_recursive() is worth one thousand words (and an upvote :-)).
1

You can try this

foreach ($array as $arr){

    array_walk_recursive($arr, function($res){
        echo $res . "\n". '<br>';
    });
}

Comments

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>';
        }
    }
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.