Skip to content

Instantly share code, notes, and snippets.

@diffmike
Last active October 15, 2015 15:24
Show Gist options
  • Select an option

  • Save diffmike/a8119f1a24359b4cda60 to your computer and use it in GitHub Desktop.

Select an option

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)
<?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