My question is how correctly convert from one element of array ['a_b_c' => 123] to ['a' => ['b' => ['c' => 123]]]; with any level!
As example input data(in tests), i use ['a_a' => '2', 'a_b' => '333', 'c_c' => '123'] as key separator i choose '_'
And waiting result like:
['a' => ['a' => '2', 'b' => '333'], 'c' => ['c' => '123']
but actual result is: ['a' => ['b' => '333'], 'c' => ['c' => '123']]
i use static function from class Helper:
/**
* @param array|string $data Inserted data, like regular array, or simple string(will return as is)
* @param string $separator string separator to know what we had.
* @return array|string
*/
public static function unFlatArr($data, $separator = '_')
{
$return = [];
if (is_iterable($data)) {
foreach ($data as $oldKey => $value) {
$keysArr = explode($separator, $oldKey, 2);
$key = $keysArr[0];
$k = $keysArr[1] ?? null;
if (null !== $k) {
//got nesting.
$value = self::unFlatAr([$k => $value], $separator);
}
$return[$key] = $value;
}
} else {
$return = $data;
}
return $return;
}
Where i'm fail and why, please, tell me. thank you.