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/28/05 01:09

see below...

"Jochem Maas" <jochem@iamjochem.com> wrote in message
news:4339BDD6.8070301@iamjochem.com...
> 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 :-)

Heh heh, nice try... I'm not that new here. I have been using this for 5
years now. Just that I don't use it often unless I have a breakdown. ;-)

>
> >
> > 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...
> >

Yea, strange the beliefs people have here. :-) Will try to play around
with the coding... I'm going to have a heck of a headache for a while. ;-)

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

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