You are here: Re: [PHP] Array - partical path to the array's path.... « PHP « IT news, forums, messages
Re: [PHP] Array - partical path to the array's path....

Posted by "Scott Fletcher" on 09/27/05 23:01

"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]
> >>
> >> This is the simple code that I'm trying to make it work. Does anyone
> >> know
> >> how does these work with array? Some help here...
> >>
> >> Thanks...
> >> FletchSOD
> >>
> >
> > 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);


> - but doesn't answer the OPs question actually imho the
What does the "OP" stand for?

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
array (shorter or longer arrays, more keys or less keys, etc). I hope. :-)

Thanks...

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация