|
12 | 12 | */
|
13 | 13 | function set_path(array &$haystack, string $path, $value, string $separator = '.'): void
|
14 | 14 | {
|
15 |
| - $key = trim($path, $separator); |
16 |
| - $keys = explode($separator, $key); |
17 |
| - if (empty($keys)) { |
18 |
| - return; |
19 |
| - } |
20 |
| - |
21 |
| - $target = &$haystack; |
22 |
| - foreach ($keys as $innerKey) { |
23 |
| - if (! is_array($target)) { |
24 |
| - break; |
25 |
| - } |
| 15 | + $path = explode($separator, $path); |
| 16 | + $temp =& $haystack; |
26 | 17 |
|
27 |
| - if (! array_key_exists($innerKey, $target)) { |
28 |
| - $target[$innerKey] = []; |
| 18 | + foreach ($path as $key) { |
| 19 | + if (!is_array($temp)) { |
| 20 | + $temp = []; |
29 | 21 | }
|
30 | 22 |
|
31 |
| - $target = &$target[$innerKey]; |
| 23 | + $temp =& $temp[$key]; |
32 | 24 | }
|
33 | 25 |
|
34 |
| - $target = $value; |
| 26 | + $temp = $value; |
| 27 | +// |
| 28 | +// $key = trim($path, $separator); |
| 29 | +// $keys = explode($separator, $key); |
| 30 | +// if (empty($keys)) { |
| 31 | +// return; |
| 32 | +// } |
| 33 | +// |
| 34 | +// $target = &$haystack; |
| 35 | +// foreach ($keys as $innerKey) { |
| 36 | +// if (! is_array($target)) { |
| 37 | +// break; |
| 38 | +// } |
| 39 | +// |
| 40 | +// if (! array_key_exists($innerKey, $target)) { |
| 41 | +// $target[$innerKey] = []; |
| 42 | +// } |
| 43 | +// |
| 44 | +// $target = &$target[$innerKey]; |
| 45 | +// } |
| 46 | +// |
| 47 | +// $target = $value; |
35 | 48 | }
|
36 | 49 |
|
37 | 50 | /**
|
38 | 51 | * @param array $haystack
|
39 | 52 | * @param string $path
|
40 | 53 | * @param $default
|
41 | 54 | * @param string $separator
|
42 |
| - * @return array|mixed|null |
| 55 | + * @return mixed |
43 | 56 | */
|
44 | 57 | function get_path(array $haystack, string $path, $default = null, string $separator = '.')
|
45 | 58 | {
|
46 |
| - $key = trim($path, $separator); |
47 |
| - $keys = explode($separator, $key); |
48 |
| - if (empty($keys)) { |
49 |
| - return $default; |
50 |
| - } |
51 |
| - |
52 |
| - $target = $haystack; |
53 |
| - foreach ($keys as $innerKey) { |
54 |
| - if (! is_array($target) || ! array_key_exists($innerKey, $target)) { |
55 |
| - return $default; |
56 |
| - } |
57 |
| - |
58 |
| - $target = $target[$innerKey]; |
59 |
| - } |
60 |
| - |
61 |
| - return $target; |
| 59 | + return |
| 60 | + array_reduce( |
| 61 | + explode($separator, $path), |
| 62 | + fn ($arr, $p) => is_array($arr) && array_key_exists($p, $arr) ? $arr[$p] : $default, |
| 63 | + $haystack |
| 64 | + ); |
| 65 | + |
| 66 | +// $path = explode($separator, $path); //if needed |
| 67 | +// $temp =& $haystack; |
| 68 | +// |
| 69 | +// foreach($path as $key) { |
| 70 | +// $temp =& $temp[$key]; |
| 71 | +// } |
| 72 | +// |
| 73 | +// return $temp; |
| 74 | +// |
| 75 | +// $key = trim($path, $separator); |
| 76 | +// $keys = explode($separator, $key); |
| 77 | +// if (empty($keys)) { |
| 78 | +// return $default; |
| 79 | +// } |
| 80 | +// |
| 81 | +// $target = $haystack; |
| 82 | +// foreach ($keys as $innerKey) { |
| 83 | +// if (! is_array($target) || ! array_key_exists($innerKey, $target)) { |
| 84 | +// return $default; |
| 85 | +// } |
| 86 | +// |
| 87 | +// $target = $target[$innerKey]; |
| 88 | +// } |
| 89 | +// |
| 90 | +// return $target; |
62 | 91 | }
|
63 | 92 |
|
64 | 93 | /**
|
|
0 commit comments