Re: Array - partical path to the array's path....

From: Date: Tue, 27 Sep 2005 21:57:15 +0000
Subject: Re: Array - partical path to the array's path....
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-223323@lists.php.net to get a copy of this message
My bad - that doesn't work - that came off the top off my head. It sure did look sexy though, no ? - MD
echo ${"array".$prefix};
really? did you test that? doesn't work when I do it (the second expression does - but doesn't answer the OPs question actually imho the answer is not eval() either, because its slow and a security headache): $array = array(); $array["col3"]["col2"] = "Test #2"; $prefix = "[\"col3\"][\"col2\"]"; echo ${"array".$prefix}, ${"array"}["col3"]["col2"]; ----------------------------- try something like this instead?: (code has been tested) function getVal($arr, $path) {
    $retval = null;
    if (!is_array($arr) || empty($arr) ||
        !is_array($path) || empty($path)) {
        return null;
    }
    while (count($path)) {
        $key = array_shift($path);
        if (!isset($arr[ $key ])) {
            return null;
        }
        $retval = $arr[ $key ];
        $arr    =& $arr[ $key ];
    }
    return $retval;
} $ra = array(); $ra["col3"]["col2"] = "Test #2"; $path = array("col3","col2"); echo getVal($ra, $path);


« previous php.general (#223323) next »