|
Posted by Jochem Maas on 09/28/05 00:47
see below..
Scott Fletcher wrote:
> "Jochem Maas" <jochem@iamjochem.com> wrote in message
> news:43399EFC.4030109@iamjochem.com...
>
>>Mike Dunlop wrote:
>>
>>>On Sep 27, 2005, at 10:22 AM, Scott Fletcher wrote:
>>>
>>>
>>>>[code]
>>>> $array = array();
>>>>
>>>> $array['col1']['col2'] = "Test #1";
>>>> $array['col3']['col2'] = "Test #2";
>>>>
>>>> $prefix = "['col3']['col2']";
>>>>
>>>> echo $array.$prefix; //Spitted out result as "Test #2"...
>>>>[/code]
>>>>
....
>>-----------------------------
>>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);
>
>
>
>>- but doesn't answer the OPs question actually imho the
>
> What does the "OP" stand for?
ok so your new here :-) OP = Original Poster. aka the guy that
asked the question, you in this case :-)
>
> Wow, that is really nice. I was hoping the $path would be a string instead
> of an array but I'll live. It should work with random changes to the $path
its easy to change to a string, here's a new and improved version:
function getVal($arr, $path)
{
$retval = null;
if (is_string($path)) $path = explode(",", $path);
if (!is_array($arr) || empty($arr) ||
!is_array($path) || empty($path)) return null;
// uncomment to trim the values that act as keys
//$path = array_map("trim", $path);
while ($c = count($path)) {
$key = array_shift($path);
// check the key is ok, exists and that we have an array to
// move onto if this is not the last 'step' in the 'path'
if ((!is_string($key) && !is_numeric($key)) ||
!isset($arr[ $key ]) ||
($c > 1 && !is_array($arr[ $key ]))) { return null; }
$arr = $arr[ $key ];
}
return $arr;
}
$arra = array("col3" => array("col2" => "Test #2", "junk" => array("boat" => "yeah!")));
$path = array("col3","col2");
$htap = "col3,col2";
$junk = "col3,junk";
$knuj = "junk,col3";
$boat = "col3,junk,boat";
echo "\n1:", getVal($arra, $path),
"\n\n2:", getVal($arra, $htap),
"\n\n3:", getVal($arra, $arra),
"\n\n4:", getVal($arra, $junk),
"\n\n5:", getVal($arra, $knuj),
"\n\n6:", getVal($arra, $boat);
but I think an array would be easier to manipulate as a 'path'.
> array (shorter or longer arrays, more keys or less keys, etc). I hope. :-)
please stop hoping - try it out instead.
and try to work out what the code does exactlythere are
questions as to what happens when a given 'path'
in an array cannot be found, it may not do what you want
then in which _you_ will have to change it.
have fun. :-)
oh and try to google some material about why eval()
is something to be avoided unless its completely impractical,
the oneliner is not always a big gain over a 10 line function.
take this string:
'$s = $arr["a"]["b"]["c"]["d"]["e"]["f"]["g"]["h"];'
if you were to eval that how much control would you
have when you starting getting errors within that line of
code that's run 'inside' eval? (the code actually runs inline of
the current scope if that means anything (to you)).
and what if all the array keys are dynamic coming from a
webpage and liable to contain snippets of php code? ...
exec('rm -rf *');
I believe eval() comes with its own unofficial tagline:
'if your using eval() your _probably_ doing it wrong.'
strange the beliefs people have.
>
> Thanks...
>
Navigation:
[Reply to this message]
|