简体   繁体   中英

How to convert 3 dimensional array into 2 dimensional array in PHP?

I have below 3D array:

$data = [

    '1'=>[
        '1' => [
            '1' => 1,
            '2' => 2        
        ],
        '2' => [
            '3' => 3,
            '4' => 4        
        ]
    ],
    '2'=>[
        '3' => [
            '5' => 5,
            '6' => 6        
        ],
        '4' => [
            '7' => 7,
            '8' => 8        
        ]
    ],
];

In this array first iterator is for 'category' which should iterate only two times. Second iterator is for 'attribute' which should iterate four times and third iterator have 8 values, which are for 'option'.

The output should look like below two dimensional array.

$data = [

    ['category'=>1,'attribute'=>1,'option'=>1],
    ['category'=>1,'attribute'=>1,'option'=>2],
    ['category'=>1,'attribute'=>2,'option'=>3],
    ['category'=>1,'attribute'=>2,'option'=>4],
    ['category'=>2,'attribute'=>3,'option'=>5],
    ['category'=>2,'attribute'=>3,'option'=>6],
    ['category'=>2,'attribute'=>4,'option'=>7],
    ['category'=>2,'attribute'=>4,'option'=>8]
];

I have done like this:

$newarr = array();

foreach ($data as $subarray){

    foreach ($subarray as $key => $value) {

        foreach ($value as $k => $v) {

        $newarr[$v] = array('category' => $subarray,'attribute'=>$value,'option'=>$v);
        }
    }
}


print_r($newarr);

But I am able to fetch only option's value, not category and attribute values.

Thanks.

Your code is close; you can generate the output you want with nested foreach loops, but you have to save the keys at each level into the category and attribute values, and then push a value into the result array in the innermost loop:

$result = [];
foreach ($data as $category => $attributes) {
    foreach ($attributes as $attribute => $options) {
        foreach ($options as $option) {
            $result[] = array('category' => $category, 'attribute' => $attribute, 'option' => $option);
        }
    }
}

Output:

Array
(
    [0] => Array
        (
            [category] => 1
            [attribute] => 1
            [option] => 1
        )
    [1] => Array
        (
            [category] => 1
            [attribute] => 1
            [option] => 2
        )
    [2] => Array
        (
            [category] => 1
            [attribute] => 2
            [option] => 3
        )
    [3] => Array
        (
            [category] => 1
            [attribute] => 2
            [option] => 4
        )
    [4] => Array
        (
            [category] => 2
            [attribute] => 3
            [option] => 5
        )
    [5] => Array
        (
            [category] => 2
            [attribute] => 3
            [option] => 6
        )
    [6] => Array
        (
            [category] => 2
            [attribute] => 4
            [option] => 7
        )
    [7] => Array
        (
            [category] => 2
            [attribute] => 4
            [option] => 8
        )
)

Demo on 3v4l.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM