Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: npisarev
- * Date: 03/12/2016
- * Time: 11:46
- */
- $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,
- ];
- $data = [
- 'parent' => [
- 'child' => [
- 'field' => 1,
- 'field2' => 2,
- ],
- ],
- 'parent2' => [
- 'child' => [
- 'name' => 'test',
- ],
- 'child2' => [
- 'name' => 'test',
- 'position' => 10,
- ],
- ],
- 'parent3' => [
- 'child3' => [
- 'position' => 10,
- ],
- ],
- ];
- function constructive(array $data)
- {
- $result = [];
- foreach ($data as $key => $value) {
- list($parent, $child, $arg) = explode('.', $key);
- $result[$parent][$child][$arg] = $value;
- }
- return $result;
- }
- function recursive(array $data, $keys = [])
- {
- $result = [];
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $keys[] = $key;
- $result = array_merge($result, recursive($value, $keys));
- array_pop($keys);
- continue;
- }
- $resKey = implode('.', $keys);
- $result[$resKey . '.' .$key] = $value;
- }
- return $result;
- }
- print_r(constructive($data1));
- print_r(recursive($data));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement