20
arrayput
array_put
SYNOPSIS
array_put($v, $keys, $value)
DESCRIPTION
array_put stores $value in the multidimensional array $v at the location designated by the sequence of keys specified by $keys.
A missing array for a key is created automatically.
If an intermediate value is not an array, array_put returns false.
If $keys is not an array or is empty, array_put returns false.
Otherwise, array_put returns $value.
EXAMPLE
To store a value in a multidimensional array:
php > require 'library/dump.php';
php > require 'library/arrayput.php';
php > $arr=array('user' => array('name' => 'izend'));
php > array_put($arr, array('user', 'mail'), 'webmaster@izend.org');
php > dump($arr);
array(1) {
["user"] => array(2) {
["name"] => string(5) "izend"
["mail"] => string(19) "webmaster@izend.org"
}
}
CODE
- function array_put(&$v, $keys, $value) {
- if (!is_array($keys) || empty($keys))
- return false;
- $array=null;
- foreach ($keys as $k) {
- if (!is_array($v))
- return false;
- if (!array_key_exists($k, $v))
- $v[$k] = array();
- $array=&$v;
- $v = &$v[$k];
- }
- return $array ? $array[$k]=$value : false;
- }
Comments