$demo = [
["group"=>"A",
"list"=>[
["name"=>"1"],
["name"=>"2"],
["name"=>"3"],]
],
["group"=>"B",
"list"=>[
["name"=>"4"],
["name"=>"5"],
["name"=>"6"],]
]
];
$middle = '';
dd(
collect($demo)->flatten()->map(
function($val) use(&$middle){
if(is_numeric($val)) {
return ['name'=>$val,'group'=>$middle];
}else{
$middle = $val;
}
})->reject(
function($val){
return !is_array($val);
}
)
);