note 22460 added to ref.array
| From: | webkid%webkid.com@php.net | Date: | Wed, 19 Jun 2002 03:47:50 +0000 |
| Subject: | note 22460 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31998@lists.php.net to get a copy of this message | ||
A painstakenly written function to add a "value" to a recursive array using a pathlist
(also in the format of an array):
function recurse($a,$rm,$k,$d){ if (sizeof($rm)==0){$a[$k]=$d;
return ($a);}
$sh=array_shift($rm); $a[$sh]=recurse ($a[$sh],$rm,$k,$d);
return ($a);}
Called like this: $ary=recurse($ary,
array("level1","level2","level3"),"key",$value);
So an array like
Array(
[0] => a
[1] => b
[2] => c
[d] => Array
( [0] => a
[1] => b
[c] => Array
( [0] => e
[1] => f
[2] => g
[3] => h
[i] => Array
( [0] => n
) ) ) )
Can become
Array(
[0] => a
[1] => b
[2] => c
[d] => Array
( [0] => a
[1] => b
[c] => Array
( [0] => e
[1] => f
[2] => g
[3] => h
[i] => Array
( [0] => n
[key] => data
) ) ) )
With the call
$ary=recurse($ary,array("d","c","i"),"key","data");
--
http://www.php.net/manual/en/ref.array.php
http://master.php.net/manage/user-notes.php?action=edit+22460
http://master.php.net/manage/user-notes.php?action=delete+22460
http://master.php.net/manage/user-notes.php?action=reject+22460