Last active
October 15, 2015 15:24
-
-
Save diffmike/a8119f1a24359b4cda60 to your computer and use it in GitHub Desktop.
array_set via dot notation. for example array_set('z.x.c.v', 6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function array_set($keys, $value, $array = []) { | |
| $keys = array_reverse(explode('.', $keys)); | |
| function step($keys, $value, $default, $array = []) { | |
| if (!$keys) return array_merge_recursive($default, $array); | |
| $keys = array_values($keys); | |
| $result[$keys[0]] = $default == $array ? $value : $array; | |
| unset($keys[0]); | |
| return step($keys, $value, $default, $result); | |
| } | |
| return step($keys, $value, $array, $array); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment