Skip to content
Advertisement

I need to remove a single array within a 2d array and form associative array

I’m getting this in my post,

[{"master":"80216","assign":["80491","80514","80575"]}]

Required Output

[{"master":"80216","assign":"80491"}]
[{"master":"80216","assign":"80514"}]
[{"master":"80216","assign":"80575"}]

Advertisement

Answer

This will do the trick. No explanation is needed. You can get it by going through the code.

$input  = []; # your data array

foreach ($input as $item) {
    $master = $item["master"];
    foreach ($item["assign"] as $assign) {
        $output[] = [
            "master" => $master,
            "assign" => $assign
        ];
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement