mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
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:
parent
73fdbfe8c5
commit
cdb8925912
1 changed files with 32 additions and 0 deletions
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue