You are here: Re: navigate multidimensional array with path in flat array « PHP Programming Language « IT news, forums, messages
Re: navigate multidimensional array with path in flat array

Posted by Rik Wasmus on 10/18/07 15:56

On Thu, 18 Oct 2007 17:32:41 +0200, Snaggy <l.cioria@gmail.com> wrote:

> I have a big multidimensional array and I need to access it at various=

> levels. The path is stored in a flat array:
>
> I'm doing it like this:
>
> $path =3D array("people", "men", "tall")
>
> $my_array =3D <---- this is the big array!
>
> foreach ($path as $x) {$p .=3D "[\"".$x."\"]" ;}
> $str =3D "$part =3D $my_array".$p;
> eval($str);
>
> which works but is very ugly, I'd like to do something like
>
> $temp =3D $my_array[$path];
>
> but it doesn't work of course.

something like:
function _get_from_array($array,$path){
$cur =3D $array;
while($key =3D array_shift($path)){
if(array_key_exists($cur,$pkey)){
$cur =3D $cur[$key];
} else {
trigger_error('array key doesn\'t exist');
return false;
}
}
return $cur;
}


$temp =3D _get_from_array($array,$path);

-- =

Rik Wasmus

 

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

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