note 81115 deleted from ref.array by levim
| From: | levim@php.net | Date: | Tue, 06 May 2014 22:10:16 +0000 |
| Subject: | note 81115 deleted from ref.array by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199467@lists.php.net to get a copy of this message | ||
Note Submitter: ms419 at freezone dot co dot uk
----
This function takes an array, a key and a value. If the key is not an array, it acts just like
$array[$key] =& $value; If the key is an array, it recurses in the array, creating nested arrays
as necessary.
Example:
$array = array('A' => array('B' => 'phi'));
$key = array('A', 'Z');
$value = 'gamma';
arraySet($array, $key, $value);
$array should now be:
array('A' => array('B' => 'phi', 'C' =>
'gamma'));
This is useful for constructing nested arrays from sets of filesystem paths (e.g. 'A/B/C')
or structured variable names (e.g. 'A.B.C' or 'A[B][C]')
Example:
arraySet($array, preg_split('/\//', $path, -1, PREG_SPLIT_NO_EMPTY), $value);
function arraySet(array &$array, $key, &$value)
{
if (is_array($key)) {
$keyComponent = array_shift($key);
if (empty($key)) {
$array[$keyComponent] =& $value;
return;
}
if (!is_array($array[$keyComponent])) {
$array[$keyComponent] = array();
}
PHP_CodeSniffer_arraySet($array[$keyComponent], $key, $value);
return;
}
$array[$key] =& $value;
}