Implementing Set::insert() for dynamic array writes

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4509 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-02-12 18:31:26 +00:00
parent 73fdbfe8c5
commit cdb8925912

View file

@ -244,6 +244,38 @@ class Set extends Object {
}
return $data;
}
/**
* Inserts $data into an array as defined by $path.
*
* @param mixed $list
* @param array $data
* @param mixed $path A dot-separated string.
* @return array
*/
function insert(&$list, $path, $data = null) {
if (empty($data) && is_a($this, 'Set')) {
$data = $path;
$path = $list;
$list = $this->get();
}
if (!is_array($path)) {
$path = explode('.', $path);
}
$_list =& $list;
foreach($path as $i => $key) {
if (intval($key) > 0 || $key == '0') {
$key = intval($key);
}
if ($i == count($path) - 1) {
$_list[$key] = $data;
} elseif (!isset($_list[$key])) {
$_list[$key] = array();
$_list =& $_list[$key];
}
}
return $list;
}
/**
* Computes the difference between a Set and an array, two Sets, or two arrays
*