php create multidimensional array from flat one
Tried this but counting backwards won't work as I need to move from start to finish in the order in which the array is originally.
I have a simple array:
0 => Item #1
1 => Item #2
2 => Item #3
I need to create an associative array from the above like so:
Item #1
=> Item #2
=> Item #3
Where each value becomes the key for the parent item. This must be done in a while loop not recursion. It MUST move forward, the loop performs a look-ahead for validation purposes, so the original order is imperative
Thanks
EDIT - this is giving me the intended result I just can't get my around how to do this in the main loop |
array:3 [
0 => "workorder"
1 => "company"
2 => "name"
]
$array['workorder'] = [];
$temp = &$array['workorder'];
$temp['company'] = [];
$temp2 = &$temp['company'];
$temp2['name'] = [];
dump($array);
exit;
EDIT 2 | Main loop
$type = current($types);
while (array_key_exists($type, $this->types)) {
$nextType = next($types);
// ... do stuff
$type = $nextType;
}
return $array;
$temp2 = $array[$type]; $array &= $temp2;but that gives me undefined index notices, and simply doesn't work. I might have to change the implementation to move backwards, wrapping each child in it's parent element - however that requires re-working a bunch of code that breaks out of the loop so please excuse my hesitation to do so :)