You are here: Re: Array of 'n' dimensions? « PHP Programming Language « IT news, forums, messages
Re: Array of 'n' dimensions?

Posted by Hendri Kurniawan on 01/10/07 00:36

Miles wrote:
> Hi all,
>
> Wondering if anyone can help me. If i have an associative array:
>
> $arr = array(
> "one" => array(1, 2, 3),
> "two" => array(5, 6),
> "three" => array(7,8,9,10)
> ....
> "n" => array(p,q,r....)
> );
>
> and I want to combine the values for each value in the associative array to
> produce one array of "n" dimensions, how is
> this best done? The number of dimensions of the array is not known until
> runtime, so addressing the array can't be done in a hard-coded fashion.
>
> At the moment, I'm using a loop to build the necessary number of array
> subscripts in a string, then eval()ing the string, as a means of both
> creating and addressing the array. Which works, but it's a sloppy approach.
> I'm just wondering if anyone has another solution to the problem?
>
> Any help appreciated. My apologies for the length of the post.
>
> Regards
> Miles
>
>

function arrMerge($arr) {
$list = array();
$keys = array_keys($arr);
foreach($keys as $each) {
if(is_array($arr[$each])) $list = array_merge($list,
arrMerge($arr[$each]));
else array_push($list, $arr[$each]);
}
return $list;
}

Hendri Kurniawan

 

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

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