note 47608 added to function.compact
| From: | pillepop2003 at yahoo dot de | Date: | Mon, 22 Nov 2004 20:26:01 +0000 |
| Subject: | note 47608 added to function.compact | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80886@lists.php.net to get a copy of this message | ||
Use the following piece of code if you want to insert a value into an array at a path that is
extracted from a string.
Example:
You have a syntax like 'a|b|c|d' which represents the array structure, and you want to
insert a value X into the array at the position
$array['a']['b']['c']['d'] = X.
<?
function array_path_insert(&$array, $path, $value)
{
$path_el = split('\|', $path);
$arr_ref =& $array;
for($i = 0; $i < sizeof($path_el); $i++)
{
$arr_ref =& $arr_ref[$path_el[$i]];
}
$arr_ref = $value;
}
$array['a']['b']['f'] = 4;
$path = 'a|b|d|e';
$value = 'hallo';
array_path_insert($array, $path, $value);
/* var_dump($array) returns:
array(1) {
["a"]=>
&array(1) {
["b"]=>
&array(2) {
["f"]=>
int(4)
["d"]=>
&array(1) {
["e"]=>
string(5) "hallo"
}
}
}
*/
?>
Rock on
Philipp
----
Manual Page -- http://www.php.net/manual/en/function.compact.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47608
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47608&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47608&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47608&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47608&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47608&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47608&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47608&report=yes
Search -- http://master.php.net/manage/user-notes.php