Reply to Re: can you foreach() two arrays at once ?

Your name:

Reply:


Posted by Rik Wasmus on 09/07/07 16:47

On Fri, 07 Sep 2007 17:57:00 +0200, J. Frank Parnell <pos@edgecity.ufo> =
=

wrote:

> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle =

> <jstucklex@attglobal.net>
> wrote:
>
>> J. Frank Parnell wrote:
>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle =

>>> <jstucklex@attglobal.net>
>>> wrote:
>>>
>>>> J. Frank Parnell wrote:
>>>>> Hello,
>>>>> So, I was wondering how to do this:
>>>>>
>>>>> foreach($foo as $k=3D>$v AND $bar as $k2=3D>$v2){
>>>>> echo '<TR><TD>$k</TD><TD>$v</TD><TD>$k2</TD><TD>$v2</TD></TR>;
>>>>> }
>>>>>
>>>>> Thanks,
>>>> No.
>>>>
>>>> However, you can use "each" to do the same thing, i.e.
>>>>
>>>> reset $array1;
>>>> reset $array2;
>>>>
>>>> for ((list($key1, $val1) =3D each($array1)) &&
>>>> (list($key2, $val2) =3D each($array2)) {
>>>> // $key1 and val1 contain the key and value for an element in $arra=
y1
>>>> // $key2 and val2 contain the key and value for an element in $arra=
y2
>>>> // Do your stuff here
>>>> }
>>>>
>>>> It will stop as soon as you run out of elements in either array.
>>>
>>> Ah, cool, I saw similar on the php.net. Is there a way to do it so =
=

>>> that it will
>>> go thru all of both, even if one runs out?
>>>
>>
>> What do you want to do with the array which runs out? And what do yo=
u
>> want to do with the array with items left?
>
> I figured the var that ran out would just be empty while the other va=
r =

> is still
> listing, eaching, etc. It might be handy to be able to specify a =

> default value
> instead of empty, like &nbsp for the table exapmle above. Looks like =
I =

> could
> just stick a little if() in Satya's code.

Or an array_pad() on the smaller one...


-- =

Rik Wasmus

[Back to original 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

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