Re: changing values deep in an array by reference, with some index vars in array..

[email protected] (Rene Veerman)
Newsgroups php.general
Message-ID <[email protected]>
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 <[email protected]> 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..
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.