Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $data1 = [
- 'parent.child.field' => 1,
- 'parent.child.field2' => 2,
- 'parent2.child.name' => 'test',
- 'parent2.child2.name' => 'test',
- 'parent2.child2.position' => 10,
- 'parent3.child3.position' => 10,
- ];
- function restructuring(array $arr) {
- $data = [];
- foreach ($arr as $k => $v) {
- $fields = explode('.', $k);
- $data[$fields[0]] = [
- $fields[1] => [
- $fields[2] => $v
- ]
- ];
- }
- return $data;
- }
- $res = restructuring($data1);
- print_r($res);
Add Comment
Please, Sign In to add comment