Breno Sabino Breno Sabino - 1 month ago 13
PHP Question

Convert multi dimensional array to organized arrays in PHP

I need to convert an array in this format:

Array (
[size] => P
[code] => Array (
[0] => C01
[1] => B01
[2] = A02
)
)


to multiple ones like this:

Array ( [size] => P [code] => C01 )
Array ( [size] => P [code] => B01 )
Array ( [size] => P [code] => A02 )


How would I do that using php code?

Answer

You need to loop throught items of code array and push values into new array or new variable.

foreach($arr["code"] as $item){
    @$newArr[] = [
        "size" => "P",
        "code" => $item
    ];
}
var_dump($newArr);

Check code result in demo