You are here: Re: can you foreach() two arrays at once ? « PHP Programming Language « IT news, forums, messages
Re: can you foreach() two arrays at once ?

Posted by Rik Wasmus on 09/08/07 00:53

On Fri, 07 Sep 2007 21:03:26 +0200, Jerry Stuckle =

<jstucklex@attglobal.net> wrote:

> Rik Wasmus wrote:
>> On Fri, 07 Sep 2007 19:13:57 +0200, Jerry Stuckle =

>> <jstucklex@attglobal.net> wrote:
>>
>>> J. Frank Parnell 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></T=
R>;
>>>>>>>> }
>>>>>>>>
>>>>>>>> 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 =

>>>>>>> $array1
>>>>>>> // $key2 and val2 contain the key and value for an element in =

>>>>>>> $array2
>>>>>>> // 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=
=

>>>>> you want to do with the array with items left?
>>>> I figured the var that ran out would just be empty while the othe=
r =

>>>> var 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 li=
ke =

>>>> I could
>>>> just stick a little if() in Satya's code.
>>>>
>>>
>>> OK, then try this:
>>>
>>> reset $array1;
>>> reset $array2;
>>>
>>> for (($val1 =3D each($array1)) || ($val2 =3D each($array2)) {
>>> if ($val1) { // false if at the end
>>> // $val1['key'] contains the key
>>> // $val1['value'] contains the value
>>> echo $val1['key'] . '=3D>' $val1['value'];
>>> }
>>> else
>>> echo '&nbsp;';
>>> if ($val2) { // false if at the end
>>> // $val2['key'] contains the key
>>> // $val2['value'] contains the value
>>> echo $val2['key'] . '=3D>' $val2['value'];
>>> }
>>> else
>>> echo '&nbsp;';
>>> }
>> Hmmz, haven't tried it, but won't the second list argument only be r=
un =

>> when the first fails, so in essence 2 foreach loops after one another=
?
>>
>
> Nope, both if statements are in the loop. Each time through it will =

> process $val1 then $val2.

<?php
$a =3D $b =3D array();
for($i =3D 1;$i<5;$i++){
$a[] =3D 'a'.$i;
$b[] =3D 'b'.$i;
}
while(($val1 =3D each($a)) || ($val2 =3D each($b))){
if (isset($val1) && $val1) { // false if at the end
// $val1['key'] contains the key
// $val1['value'] contains the value
var_dump($val1);
}
else
echo 'a not set';
if (isset($val2) && $val2) { // false if at the end
// $val2['key'] contains the key
// $val2['value'] contains the value
var_dump($val2);
}
else
echo 'b not set';
}
?>
Output:
array(4) {
[1]=3D>
string(2) "a1"
["value"]=3D>
string(2) "a1"
[0]=3D>
int(0)
["key"]=3D>
int(0)
}
b not setarray(4) {
[1]=3D>
string(2) "a2"
["value"]=3D>
string(2) "a2"
[0]=3D>
int(1)
["key"]=3D>
int(1)
}
b not setarray(4) {
[1]=3D>
string(2) "a3"
["value"]=3D>
string(2) "a3"
[0]=3D>
int(2)
["key"]=3D>
int(2)
}
b not setarray(4) {
[1]=3D>
string(2) "a4"
["value"]=3D>
string(2) "a4"
[0]=3D>
int(3)
["key"]=3D>
int(3)
}
b not seta not setarray(4) {
[1]=3D>
string(2) "b1"
["value"]=3D>
string(2) "b1"
[0]=3D>
int(0)
["key"]=3D>
int(0)
}
a not setarray(4) {
[1]=3D>
string(2) "b2"
["value"]=3D>
string(2) "b2"
[0]=3D>
int(1)
["key"]=3D>
int(1)
}
a not setarray(4) {
[1]=3D>
string(2) "b3"
["value"]=3D>
string(2) "b3"
[0]=3D>
int(2)
["key"]=3D>
int(2)
}
a not setarray(4) {
[1]=3D>
string(2) "b4"
["value"]=3D>
string(2) "b4"
[0]=3D>
int(3)
["key"]=3D>
int(3)
}

So, the original argument still stands: the second expression will not b=
e =

evaluated in this conditional as long as the first will return true.
-- =

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

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