And I need to get something like this :
Do you know any method to do this ? I'm using Laravel 8, if it helps.
Thanks !
CodePudding user response:
Something like this
$arr = [
'key1.key2.key3' => [
'field1' => 'value1',
],
'key3.key4.key5' => [
'field2' => 'value2',
],
'key6' => [
'field3' => 'value3'
]
];
$separator = '.';
$newKey = [];
foreach ($arr as $key => $value) {
$keys = explode($separator, $key);
$n = count($keys);
$keys[$n] = $value;
for ($i = 0; $i < $n; $i ) {
$keys[$i] = ["{$keys[$i]}" => &$keys[$i 1]];
}
$newKey[] = $keys[0];
}
Array
(
[0] => Array
(
[key1] => Array
(
[key2] => Array
(
[key3] => Array
(
[field1] => value1
)
)
)
)
[1] => Array
(
[key3] => Array
(
[key4] => Array
(
[key5] => Array
(
[field2] => value2
)
)
)
)
[2] => Array
(
[key6] => Array
(
[field3] => value3
)
)
)
CodePudding user response:
I think that Arr::set is what you need