You are here: Re: Recurse an array with an object/s « PHP Programming Language « IT news, forums, messages
Re: Recurse an array with an object/s

Posted by Rik on 02/07/07 16:00

Aggelos <djjelly@gmail.com> wrote:

>> function objects_from_array($var){
>> $return =3D array();
>> if(is_object($var)){
>> $return[] =3D $var;
>> }
>> if(is_array($var)){
>> $return =3D array_merge($return,objects_from_array($v=
ar));

Oops,
if(is_array($var){
foreach($var as $item){
$return =3D array_merge($return, objects_from_array($item));
}
}

>> }
>> return $return;}
>>
> That function will also enter an endless loop like petesprc

No, it won't like this :P. It will loop through an array and if it's don=
e, =

it will return all objects found. Unless you're doing something very =

strange like putting in a reference to a parent array in a sub array.


> We need to loop inside the array to get the next dimension... don't
> we ? so when you do arraymerge the $var should be the second dimension=

> of the array in the first call of the function.


> my array is like that
> $productArray[$productTypeId][$productId][$published] =3D new
> objectProduct();
>
> so for that i would do 3 nested foreach loops ...

Or just a recursive function.

-- =

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

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