changing values deep in an array by reference, with some index vars
am 11.03.2010 09:35:07 von Rene Veerman
Hi..
I've got a db-insert command array that's several levels deep..
Let's abstract this as:
$wm[$idx1][$idx2][$idx3][..etc] //$WorkMemory
Several of my helper functions need to work on the "original,
top-call_level" $wm.
So i pass it as &$wm, and have the helper function declarations accept
it as such aswell.
Some of the $idxN vars are in an array $pathToDBcmd ($idx3, $idx4,
....) in some of the helper functions.
Now i need to change a variable deep in the original, top-call_level
$wm, but with some of the required indexes in $pathToDBcmd..
I can't use something like
$wm[$idx1][$idx2][$pathToDBcmd[0]][$pathToDBcmd[1]] because i dont
know what count($pathToDBcmd) is..
Tips will be much appreciated..
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: changing values deep in an array by reference, with some index
am 11.03.2010 11:57:17 von Rene Veerman
ah, comments for
http://www.php.net/manual/en/language.references.return.php show:
------------------------------------------------------------ -----------------
php at thunder-2000 dot com
02-Feb-2007 10:31
If you want to get a part of an array to manipulate, you can use this function
function &getArrayField(&$array,$path) {
if (!empty($path)) {
if (empty($array[$path[0]])) return NULL;
else return getArrayField($array[$path[0]], array_slice($path, 1));
} else {
return $array;
}
}
Use it like this:
$partArray =& getArrayField($GLOBALS,array("config","modul1"));
You can manipulate $partArray and the changes are also made with $GLOBALS.
------------------------------------------------------------ -----------------
On Thu, Mar 11, 2010 at 9:35 AM, Rene Veerman wrote:
> Hi..
>
> I've got a db-insert command array that's several levels deep..
> Let's abstract this as:
> $wm[$idx1][$idx2][$idx3][..etc] //$WorkMemory
>
> Several of my helper functions need to work on the "original,
> top-call_level" $wm.
> So i pass it as &$wm, and have the helper function declarations accept
> it as such aswell.
>
> Some of the $idxN vars are in an array $pathToDBcmd ($idx3, $idx4,
> ...) in some of the helper functions.
>
> Now i need to change a variable deep in the original, top-call_level
> $wm, but with some of the required indexes in $pathToDBcmd..
>
> I can't use something like
> $wm[$idx1][$idx2][$pathToDBcmd[0]][$pathToDBcmd[1]] because i dont
> know what count($pathToDBcmd) is..
>
> Tips will be much appreciated..
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php