Currently I create the following nested array:
$data = // data from DB
$nested_array = [];
$counter = 0;
foreach ($data as $row) {
$nested_array[$row->a][$row->b][$row->c]['id'] = $counter++;
}
In the above case I know the amount of nesting levels I want to use, i.e. a,b,c
, which are some columns selected from the data
But I want to create the array based on an array of columns that I get. For example if I get:
$columns_selected = ['a', 'b', 'c', 'd', 'e'];
then the nested array should be:
$nested_array[$row->a][$row->b][$row->c][$row->d][$row->e]['id'] = $counter++;
evil-ish references in an assigner function could work, something like
function assigner(array &$arr, array $keys, $value = null, bool $onlyFetch = false){
$target = &$arr;
foreach($keys as $key){
$target = &$target[$key];
}
$originalValue = $target;
if(!$onlyFetch){
$target = $value;
}
return $originalValue;
}
now doing
assigner($nested_array, ['a', 'b', 'c', 'd', 'e','id'], "test");
does the same as writing
$nested_array["a"]["b"]["c"]["d"]["e"]["id"] = "test";
3v4l example: https://3v4l.org/#live
id
), whereas the others are columns from the db ($row->x
). I might just edit the function with if
condition for the final key or some sort (and pass the $data
array) assigner($nested_array, [...$columns_selected, "id"], $value);
- or you could alter the last line to be $target["id"] = $value;
- then your assigner function will only be able to write the "id" property though. $row
from the main foreach
so that I can access the data as seen in the question (maybe something like $target = &$target[$row->$key];
)