$currentKey = '';
$newArray = array();
foreach ($array as $val) {
if (!is_array($val)) {
$currentKey = $val;
} else {
if (isset($newArray[$currentKey]) && is_array($newArray[$currentKey])) {
$newArray[$currentKey][] = $val;
} else {
$newArray[$currentKey] = array($val);
}
}
}
I hope you understand what is happening here?
And of course it’ll work only with arrays like first from your question.
1
solved PHP reformatting array