can you foreach() two arrays at once ?

can you foreach() two arrays at once ?

am 07.09.2007 02:52:21 von pos

Hello,
So, I was wondering how to do this:

foreach($foo as $k=>$v AND $bar as $k2=>$v2){
echo '$k$v$k2$v2;
}

Thanks,

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

am 07.09.2007 03:41:27 von Jerry Stuckle

J. Frank Parnell wrote:
> Hello,
> So, I was wondering how to do this:
>
> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
> echo '$k$v$k2$v2;
> }
>
> Thanks,

No.

However, you can use "each" to do the same thing, i.e.

reset $array1;
reset $array2;

for ((list($key1, $val1) = each($array1)) &&
(list($key2, $val2) = 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.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 07.09.2007 05:49:41 von pos

On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
wrote:

>J. Frank Parnell wrote:
>> Hello,
>> So, I was wondering how to do this:
>>
>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>> echo '$k$v$k2$v2;
>> }
>>
>> Thanks,
>
>No.
>
>However, you can use "each" to do the same thing, i.e.
>
>reset $array1;
>reset $array2;
>
>for ((list($key1, $val1) = each($array1)) &&
> (list($key2, $val2) = 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?

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

am 07.09.2007 09:28:37 von satya

On Sep 7, 8:49 am, J. Frank Parnell wrote:
> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
> wrote:
>
>
>
> >J. Frank Parnell wrote:
> >> Hello,
> >> So, I was wondering how to do this:
>
> >> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
> >> echo '$k$v$k2$v2;
> >> }
>
> >> Thanks,
>
> >No.
>
> >However, you can use "each" to do the same thing, i.e.
>
> >reset $array1;
> >reset $array2;
>
> >for ((list($key1, $val1) = each($array1)) &&
> > (list($key2, $val2) = 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?


if (count ($arr1) > count($arr2)) {
$len = count($arr1);
}
else {
$len = count($arr2);
}

Now

foreach($i=0; $i < $len; $i++) {

list($key1, $val1) = @each($arr1);
list($key2, $val2) = @each($arr2);
}

What about this?
I have not checked this.

http://satya61229.blogspot.com/

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

am 07.09.2007 09:29:57 von satya

On Sep 7, 12:28 pm, Satya wrote:
> On Sep 7, 8:49 am, J. Frank Parnell wrote:
>
>
>
> > On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
> > wrote:
>
> > >J. Frank Parnell wrote:
> > >> Hello,
> > >> So, I was wondering how to do this:
>
> > >> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
> > >> echo '$k$v$k2$v2;
> > >> }
>
> > >> Thanks,
>
> > >No.
>
> > >However, you can use "each" to do the same thing, i.e.
>
> > >reset $array1;
> > >reset $array2;
>
> > >for ((list($key1, $val1) = each($array1)) &&
> > > (list($key2, $val2) = 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?
>
> if (count ($arr1) > count($arr2)) {
> $len = count($arr1);}
>
> else {
> $len = count($arr2);
>
> }
>
> Now
>
> for($i=0; $i < $len; $i++) {
>
> list($key1, $val1) = @each($arr1);
> list($key2, $val2) = @each($arr2);
>
> }
>
> What about this?
> I have not checked this.
>
> http://satya61229.blogspot.com/

There I used "foreach" instead of "for".

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

am 07.09.2007 13:22:46 von Jerry Stuckle

J. Frank Parnell wrote:
> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
> wrote:
>
>> J. Frank Parnell wrote:
>>> Hello,
>>> So, I was wondering how to do this:
>>>
>>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>>> echo '$k$v$k2$v2;
>>> }
>>>
>>> Thanks,
>> No.
>>
>> However, you can use "each" to do the same thing, i.e.
>>
>> reset $array1;
>> reset $array2;
>>
>> for ((list($key1, $val1) = each($array1)) &&
>> (list($key2, $val2) = 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?



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 07.09.2007 17:57:00 von pos

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

>J. Frank Parnell wrote:
>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>> wrote:
>>
>>> J. Frank Parnell wrote:
>>>> Hello,
>>>> So, I was wondering how to do this:
>>>>
>>>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>>>> echo '$k$v$k2$v2;
>>>> }
>>>>
>>>> Thanks,
>>> No.
>>>
>>> However, you can use "each" to do the same thing, i.e.
>>>
>>> reset $array1;
>>> reset $array2;
>>>
>>> for ((list($key1, $val1) = each($array1)) &&
>>> (list($key2, $val2) = 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 other var is still
listing, eaching, etc. It might be handy to be able to specify a default value
instead of empty, like   for the table exapmle above. Looks like I could
just stick a little if() in Satya's code.

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

am 07.09.2007 18:47:47 von luiheidsgoeroe

On Fri, 07 Sep 2007 17:57:00 +0200, J. Frank Parnell =
=

wrote:

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

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

>>>
>>> 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 '$k$v$k2$v2;
>>>>> }
>>>>>
>>>>> 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   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

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

am 07.09.2007 19:13:57 von Jerry Stuckle

J. Frank Parnell wrote:
> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
> wrote:
>
>> J. Frank Parnell wrote:
>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>> wrote:
>>>
>>>> J. Frank Parnell wrote:
>>>>> Hello,
>>>>> So, I was wondering how to do this:
>>>>>
>>>>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>>>>> echo '$k$v$k2$v2;
>>>>> }
>>>>>
>>>>> Thanks,
>>>> No.
>>>>
>>>> However, you can use "each" to do the same thing, i.e.
>>>>
>>>> reset $array1;
>>>> reset $array2;
>>>>
>>>> for ((list($key1, $val1) = each($array1)) &&
>>>> (list($key2, $val2) = 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 other var is still
> listing, eaching, etc. It might be handy to be able to specify a default value
> instead of empty, like   for the table exapmle above. Looks like I could
> just stick a little if() in Satya's code.
>
>

OK, then try this:

reset $array1;
reset $array2;

for (($val1 = each($array1)) || ($val2 = each($array2)) {
if ($val1) { // false if at the end
// $val1['key'] contains the key
// $val1['value'] contains the value
echo $val1['key'] . '=>' $val1['value'];
}
else
echo ' ';
if ($val2) { // false if at the end
// $val2['key'] contains the key
// $val2['value'] contains the value
echo $val2['key'] . '=>' $val2['value'];
}
else
echo ' ';
}


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 07.09.2007 19:56:51 von luiheidsgoeroe

On Fri, 07 Sep 2007 19:13:57 +0200, Jerry Stuckle =

wrote:

> J. Frank Parnell wrote:
>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle =

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

>>>>
>>>> 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 '$k$v$k2$v2;
>>>>>> }
>>>>>>
>>>>>> 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 $arr=
ay1
>>>>> // $key2 and val2 contain the key and value for an element in $arr=
ay2
>>>>> // 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 y=
ou =

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

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

>> default value
>> instead of empty, like   for the table exapmle above. Looks like=
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 ' ';
> 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 ' ';
> }

Hmmz, haven't tried it, but won't the second list argument only be run =

when the first fails, so in essence 2 foreach loops after one another?


-- =

Rik Wasmus

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

am 07.09.2007 20:22:34 von pos

On Fri, 07 Sep 2007 18:47:47 +0200, "Rik Wasmus"
wrote:

>On Fri, 07 Sep 2007 17:57:00 +0200, J. Frank Parnell
>wrote:
>
>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>
>> wrote:
>>
>>> J. Frank Parnell wrote:
>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>
>>>> wrote:
>>>>
>>>>> J. Frank Parnell wrote:
>>>>>> Hello,
>>>>>> So, I was wondering how to do this:
>>>>>>
>>>>>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>>>>>> echo '$k$v$k2$v2;
>>>>>> }
>>>>>>
>>>>>> Thanks,
>>>>> No.
>>>>>
>>>>> However, you can use "each" to do the same thing, i.e.
>>>>>
>>>>> reset $array1;
>>>>> reset $array2;
>>>>>
>>>>> for ((list($key1, $val1) = each($array1)) &&
>>>>> (list($key2, $val2) = 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 other var
>> is still
>> listing, eaching, etc. It might be handy to be able to specify a
>> default value
>> instead of empty, like   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...

Well, neither of these are working, Jerry's (both scripts) have a syntax error,
(missing ; and I cant figure where it wants it) and Satya's outputs:
0 Array 0 0
1 Array 1 1
2 Array 2 2
3 Array 3 3
4 4

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

am 07.09.2007 21:01:24 von Jerry Stuckle

Jerry Stuckle wrote:
> J. Frank Parnell wrote:
>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>
>> wrote:
>>
>>> J. Frank Parnell wrote:
>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>
>>>> wrote:
>>>>
>>>>> J. Frank Parnell wrote:
>>>>>> Hello,
>>>>>> So, I was wondering how to do this:
>>>>>>
>>>>>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>>>>>> echo '$k$v$k2$v2;
>>>>>> }
>>>>>>
>>>>>> Thanks,
>>>>> No.
>>>>>
>>>>> However, you can use "each" to do the same thing, i.e.
>>>>>
>>>>> reset $array1;
>>>>> reset $array2;
>>>>>
>>>>> for ((list($key1, $val1) = each($array1)) &&
>>>>> (list($key2, $val2) = 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 other
>> var is still
>> listing, eaching, etc. It might be handy to be able to specify a
>> default value
>> instead of empty, like   for the table exapmle above. Looks like
>> I could
>> just stick a little if() in Satya's code.
>>
>>
>
> OK, then try this:
>
> reset $array1;
> reset $array2;
>
> for (($val1 = each($array1)) || ($val2 = each($array2)) {
> if ($val1) { // false if at the end
> // $val1['key'] contains the key
> // $val1['value'] contains the value
> echo $val1['key'] . '=>' $val1['value'];
> }
> else
> echo ' ';
> if ($val2) { // false if at the end
> // $val2['key'] contains the key
> // $val2['value'] contains the value
> echo $val2['key'] . '=>' $val2['value'];
> }
> else
> echo ' ';
> }
>
>

Sorry - that should be:

for (($val1 = each($array1)) || ($val2 = each($array2))) {

Missed an extra paren at the end.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 07.09.2007 21:03:26 von Jerry Stuckle

Rik Wasmus wrote:
> On Fri, 07 Sep 2007 19:13:57 +0200, Jerry Stuckle
> wrote:
>
>> J. Frank Parnell wrote:
>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>>
>>> wrote:
>>>
>>>> J. Frank Parnell wrote:
>>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>>
>>>>> wrote:
>>>>>
>>>>>> J. Frank Parnell wrote:
>>>>>>> Hello,
>>>>>>> So, I was wondering how to do this:
>>>>>>>
>>>>>>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>>>>>>> echo '$k$v$k2$v2;
>>>>>>> }
>>>>>>>
>>>>>>> Thanks,
>>>>>> No.
>>>>>>
>>>>>> However, you can use "each" to do the same thing, i.e.
>>>>>>
>>>>>> reset $array1;
>>>>>> reset $array2;
>>>>>>
>>>>>> for ((list($key1, $val1) = each($array1)) &&
>>>>>> (list($key2, $val2) = 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 other
>>> var is still
>>> listing, eaching, etc. It might be handy to be able to specify a
>>> default value
>>> instead of empty, like   for the table exapmle above. Looks like
>>> I could
>>> just stick a little if() in Satya's code.
>>>
>>
>> OK, then try this:
>>
>> reset $array1;
>> reset $array2;
>>
>> for (($val1 = each($array1)) || ($val2 = each($array2)) {
>> if ($val1) { // false if at the end
>> // $val1['key'] contains the key
>> // $val1['value'] contains the value
>> echo $val1['key'] . '=>' $val1['value'];
>> }
>> else
>> echo ' ';
>> if ($val2) { // false if at the end
>> // $val2['key'] contains the key
>> // $val2['value'] contains the value
>> echo $val2['key'] . '=>' $val2['value'];
>> }
>> else
>> echo ' ';
>> }
>
> Hmmz, haven't tried it, but won't the second list argument only be run
> 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.

Your suggestion of array_pad() is also good, unless the arrays are large.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 07.09.2007 23:40:49 von pos

On Fri, 07 Sep 2007 15:01:24 -0400, Jerry Stuckle
wrote:

>Jerry Stuckle wrote:
>> J. Frank Parnell wrote:
>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>>
>>> wrote:
>>>
>>>> J. Frank Parnell wrote:
>>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>>
>>>>> wrote:
>>>>>
>>>>>> J. Frank Parnell wrote:
>>>>>>> Hello,
>>>
>> reset $array1;
>> reset $array2;
>>
>> for (($val1 = each($array1)) || ($val2 = each($array2)) {
>> if ($val1) { // false if at the end
>> // $val1['key'] contains the key
>> // $val1['value'] contains the value
>> echo $val1['key'] . '=>' $val1['value'];
>> }
>> else
>> echo ' ';
>> if ($val2) { // false if at the end
>> // $val2['key'] contains the key
>> // $val2['value'] contains the value
>> echo $val2['key'] . '=>' $val2['value'];
>> }
>> else
>> echo ' ';
>> }
>>
>>
>
>Sorry - that should be:
>
>for (($val1 = each($array1)) || ($val2 = each($array2))) {
>
>Missed an extra paren at the end.

Thanks again for the help, however, I still get:
Parse error: syntax error, unexpected ')', expecting ';' in
/web/sites/asdf/asdf.com/_impexp/test.php on line 55

Isnt there supposed to be 3 expressions in a FOR? I tried with semicolons here
and there, but couldnt get it.

current code:
for (($val1 = each($users)) || ($val2 = each($newusers))) {
if ($val1) { // false if at the end
// $val1['key'] contains the key
// $val1['value'] contains the value
echo $val1['key'] . '=>' $val1['value'];
}
else
echo ' ';
if ($val2) { // false if at the end
// $val2['key'] contains the key
// $val2['value'] contains the value
echo $val2['key'] . '=>' $val2['value'];
}
else
echo ' ';
}

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

am 08.09.2007 00:17:48 von Jerry Stuckle

J. Frank Parnell wrote:
> On Fri, 07 Sep 2007 15:01:24 -0400, Jerry Stuckle
> wrote:
>
>> Jerry Stuckle wrote:
>>> J. Frank Parnell wrote:
>>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>>>
>>>> wrote:
>>>>
>>>>> J. Frank Parnell wrote:
>>>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>>>
>>>>>> wrote:
>>>>>>
>>>>>>> J. Frank Parnell wrote:
>>>>>>>> Hello,
>>> reset $array1;
>>> reset $array2;
>>>
>>> for (($val1 = each($array1)) || ($val2 = each($array2)) {
>>> if ($val1) { // false if at the end
>>> // $val1['key'] contains the key
>>> // $val1['value'] contains the value
>>> echo $val1['key'] . '=>' $val1['value'];
>>> }
>>> else
>>> echo ' ';
>>> if ($val2) { // false if at the end
>>> // $val2['key'] contains the key
>>> // $val2['value'] contains the value
>>> echo $val2['key'] . '=>' $val2['value'];
>>> }
>>> else
>>> echo ' ';
>>> }
>>>
>>>
>> Sorry - that should be:
>>
>> for (($val1 = each($array1)) || ($val2 = each($array2))) {
>>
>> Missed an extra paren at the end.
>
> Thanks again for the help, however, I still get:
> Parse error: syntax error, unexpected ')', expecting ';' in
> /web/sites/asdf/asdf.com/_impexp/test.php on line 55
>
> Isnt there supposed to be 3 expressions in a FOR? I tried with semicolons here
> and there, but couldnt get it.
>
> current code:
> for (($val1 = each($users)) || ($val2 = each($newusers))) {
> if ($val1) { // false if at the end
> // $val1['key'] contains the key
> // $val1['value'] contains the value
> echo $val1['key'] . '=>' $val1['value'];
> }
> else
> echo ' ';
> if ($val2) { // false if at the end
> // $val2['key'] contains the key
> // $val2['value'] contains the value
> echo $val2['key'] . '=>' $val2['value'];
> }
> else
> echo ' ';
> }
>
>
>

Sorry - it should be a 'while' loop, not a 'for' loop.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 08.09.2007 02:53:20 von luiheidsgoeroe

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

wrote:

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

>> wrote:
>>
>>> J. Frank Parnell wrote:
>>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle =

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

>>>>>>
>>>>>> 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 '$k$v$k2$v2 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   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 ' ';
>>> 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 ' ';
>>> }
>> 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.

$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

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

am 08.09.2007 03:31:46 von Jerry Stuckle

Rik Wasmus wrote:
> On Fri, 07 Sep 2007 21:03:26 +0200, Jerry Stuckle
> wrote:
>
>> Rik Wasmus wrote:
>>> On Fri, 07 Sep 2007 19:13:57 +0200, Jerry Stuckle
>>> wrote:
>>>
>>>> J. Frank Parnell wrote:
>>>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>>>>
>>>>> wrote:
>>>>>
>>>>>> J. Frank Parnell wrote:
>>>>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>>>>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> J. Frank Parnell wrote:
>>>>>>>>> Hello,
>>>>>>>>> So, I was wondering how to do this:
>>>>>>>>>
>>>>>>>>> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
>>>>>>>>> echo '$k$v$k2$v2;
>>>>>>>>> }
>>>>>>>>>
>>>>>>>>> Thanks,
>>>>>>>> No.
>>>>>>>>
>>>>>>>> However, you can use "each" to do the same thing, i.e.
>>>>>>>>
>>>>>>>> reset $array1;
>>>>>>>> reset $array2;
>>>>>>>>
>>>>>>>> for ((list($key1, $val1) = each($array1)) &&
>>>>>>>> (list($key2, $val2) = 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
>>>>> other var is still
>>>>> listing, eaching, etc. It might be handy to be able to specify a
>>>>> default value
>>>>> instead of empty, like   for the table exapmle above. Looks
>>>>> like I could
>>>>> just stick a little if() in Satya's code.
>>>>>
>>>>
>>>> OK, then try this:
>>>>
>>>> reset $array1;
>>>> reset $array2;
>>>>
>>>> for (($val1 = each($array1)) || ($val2 = each($array2)) {
>>>> if ($val1) { // false if at the end
>>>> // $val1['key'] contains the key
>>>> // $val1['value'] contains the value
>>>> echo $val1['key'] . '=>' $val1['value'];
>>>> }
>>>> else
>>>> echo ' ';
>>>> if ($val2) { // false if at the end
>>>> // $val2['key'] contains the key
>>>> // $val2['value'] contains the value
>>>> echo $val2['key'] . '=>' $val2['value'];
>>>> }
>>>> else
>>>> echo ' ';
>>>> }
>>> Hmmz, haven't tried it, but won't the second list argument only be
>>> run 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.
>
> > $a = $b = array();
> for($i = 1;$i<5;$i++){
> $a[] = 'a'.$i;
> $b[] = 'b'.$i;
> }
> while(($val1 = each($a)) || ($val2 = 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]=>
> string(2) "a1"
> ["value"]=>
> string(2) "a1"
> [0]=>
> int(0)
> ["key"]=>
> int(0)
> }
> b not setarray(4) {
> [1]=>
> string(2) "a2"
> ["value"]=>
> string(2) "a2"
> [0]=>
> int(1)
> ["key"]=>
> int(1)
> }
> b not setarray(4) {
> [1]=>
> string(2) "a3"
> ["value"]=>
> string(2) "a3"
> [0]=>
> int(2)
> ["key"]=>
> int(2)
> }
> b not setarray(4) {
> [1]=>
> string(2) "a4"
> ["value"]=>
> string(2) "a4"
> [0]=>
> int(3)
> ["key"]=>
> int(3)
> }
> b not seta not setarray(4) {
> [1]=>
> string(2) "b1"
> ["value"]=>
> string(2) "b1"
> [0]=>
> int(0)
> ["key"]=>
> int(0)
> }
> a not setarray(4) {
> [1]=>
> string(2) "b2"
> ["value"]=>
> string(2) "b2"
> [0]=>
> int(1)
> ["key"]=>
> int(1)
> }
> a not setarray(4) {
> [1]=>
> string(2) "b3"
> ["value"]=>
> string(2) "b3"
> [0]=>
> int(2)
> ["key"]=>
> int(2)
> }
> a not setarray(4) {
> [1]=>
> string(2) "b4"
> ["value"]=>
> string(2) "b4"
> [0]=>
> int(3)
> ["key"]=>
> int(3)
> }
>
> So, the original argument still stands: the second expression will not
> be evaluated in this conditional as long as the first will return true.

Darn, you're right - I've been in vbscript too much recently (don't ask!
:-)). And it evaluates the entire expression.

Better would be:

for ($val1 = each($a), $val2 = each($b); $val1 || $val2;
$val1 = each($a), $val2 = each($b))

Does that make you happy? :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 08.09.2007 03:34:22 von Jerry Stuckle

Jerry Stuckle wrote:
> J. Frank Parnell wrote:
>> On Fri, 07 Sep 2007 15:01:24 -0400, Jerry Stuckle
>>
>> wrote:
>>
>>> Jerry Stuckle wrote:
>>>> J. Frank Parnell wrote:
>>>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>>>>
>>>>> wrote:
>>>>>
>>>>>> J. Frank Parnell wrote:
>>>>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>>>>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> J. Frank Parnell wrote:
>>>>>>>>> Hello,
>>>> reset $array1;
>>>> reset $array2;
>>>>
>>>> for (($val1 = each($array1)) || ($val2 = each($array2)) {
>>>> if ($val1) { // false if at the end
>>>> // $val1['key'] contains the key
>>>> // $val1['value'] contains the value
>>>> echo $val1['key'] . '=>' $val1['value'];
>>>> }
>>>> else
>>>> echo ' ';
>>>> if ($val2) { // false if at the end
>>>> // $val2['key'] contains the key
>>>> // $val2['value'] contains the value
>>>> echo $val2['key'] . '=>' $val2['value'];
>>>> }
>>>> else
>>>> echo ' ';
>>>> }
>>>>
>>>>
>>> Sorry - that should be:
>>>
>>> for (($val1 = each($array1)) || ($val2 = each($array2))) {
>>>
>>> Missed an extra paren at the end.
>>
>> Thanks again for the help, however, I still get:
>> Parse error: syntax error, unexpected ')', expecting ';' in
>> /web/sites/asdf/asdf.com/_impexp/test.php on line 55
>>
>> Isnt there supposed to be 3 expressions in a FOR? I tried with
>> semicolons here
>> and there, but couldnt get it.
>> current code:
>> for (($val1 = each($users)) || ($val2 = each($newusers))) {
>> if ($val1) { // false if at the end
>> // $val1['key'] contains the key
>> // $val1['value'] contains the value
>> echo $val1['key'] . '=>' $val1['value'];
>> }
>> else
>> echo ' ';
>> if ($val2) { // false if at the end
>> // $val2['key'] contains the key
>> // $val2['value'] contains the value
>> echo $val2['key'] . '=>' $val2['value'];
>> }
>> else
>> echo ' ';
>> }
>>
>>
>>
>
> Sorry - it should be a 'while' loop, not a 'for' loop.
>

Also, see the correction above in response to Rik's comments:

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 08.09.2007 10:30:37 von gosha bine

J. Frank Parnell wrote:
> Hello,
> So, I was wondering how to do this:
>
> foreach($foo as $k=>$v AND $bar as $k2=>$v2){
> echo '$k$v$k2$v2;
> }
>

try

$a = array('a1' => 1, 'a2' => 2);
$b = array('b1' => 11, 'b2' => 22, 'c2' => 33);

$keys = array_map(null, array_keys($a), array_keys($b));

foreach($keys as $k) {
list($k1, $k2) = $k;
$v1 = isset($k1) ? $a[$k1] : null;
$v2 = isset($k2) ? $b[$k2] : null;
echo "$k1 - $v1 - $k2 - $v2 \n";
}

if you don't need keys, this can be simplified to

foreach(array_map(null, $a, $b) as $v) {
list($v1, $v2) = $v;
echo "$v1 - $v2 \n";
}


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

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

am 11.09.2007 01:49:39 von pos

On Fri, 07 Sep 2007 21:34:22 -0400, Jerry Stuckle
wrote:

>Jerry Stuckle wrote:
>> J. Frank Parnell wrote:
>>> On Fri, 07 Sep 2007 15:01:24 -0400, Jerry Stuckle
>>>
>>> wrote:
>>>
>>>> Jerry Stuckle wrote:
>>>>> J. Frank Parnell wrote:
>>>>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>>>>>
>>>>>> wrote:
>>>>>>
>>>>>>> J. Frank Parnell wrote:
>>>>>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>>>>>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> J. Frank Parnell wrote:
>>>>>>>>>> Hello,
>>>>> reset $array1;
>>>>> reset $array2;
>>>>>
>>>>> for (($val1 = each($array1)) || ($val2 = each($array2)) {
>>>>> if ($val1) { // false if at the end
>>>>> // $val1['key'] contains the key
>>>>> // $val1['value'] contains the value
>>>>> echo $val1['key'] . '=>' $val1['value'];
>>>>> }
>>>>> else
>>>>> echo ' ';
>>>>> if ($val2) { // false if at the end
>>>>> // $val2['key'] contains the key
>>>>> // $val2['value'] contains the value
>>>>> echo $val2['key'] . '=>' $val2['value'];
>>>>> }
>>>>> else
>>>>> echo ' ';
>>>>> }
>>>>>
>>>>>
>>>> Sorry - that should be:
>>>>
>>>> for (($val1 = each($array1)) || ($val2 = each($array2))) {
>>>>
>>>> Missed an extra paren at the end.
>>>
>>> Thanks again for the help, however, I still get:
>>> Parse error: syntax error, unexpected ')', expecting ';' in
>>> /web/sites/asdf/asdf.com/_impexp/test.php on line 55
>>>
>>> Isnt there supposed to be 3 expressions in a FOR? I tried with
>>> semicolons here
>>> and there, but couldnt get it.
>>> current code:
>>> for (($val1 = each($users)) || ($val2 = each($newusers))) {
>>> if ($val1) { // false if at the end
>>> // $val1['key'] contains the key
>>> // $val1['value'] contains the value
>>> echo $val1['key'] . '=>' $val1['value'];
>>> }
>>> else
>>> echo ' ';
>>> if ($val2) { // false if at the end
>>> // $val2['key'] contains the key
>>> // $val2['value'] contains the value
>>> echo $val2['key'] . '=>' $val2['value'];
>>> }
>>> else
>>> echo ' ';
>>> }
>>>
>>>
>>>
>>
>> Sorry - it should be a 'while' loop, not a 'for' loop.
>>
>
>Also, see the correction above in response to Rik's comments:

OK, got it thanks. Everybody, Rik, Jerry, Gosha and Satya...I appreciate it.

Now, I go to test things and of course, I forgot one big detail. The two arrays
are actually multidimensional themselves:
$a[0] = array('a[0]key1' => 'a[0]val1', 'a[0]key2' => 'a[0]val2');
$a[1] = array('a[1]key1' => 'a[1]val1', 'a[1]key2' => 'a[1]val2');
$b[0] = array('b[0]key1' => 'b[0]val1', 'b[0]key2' => 'b[0]val2', 'b[0]key3' =>
'b[0]val3');
$b[1] = array('b[1]key1' => 'b[1]val1', 'b[1]key2' => 'b[1]val2', 'b[1]key3' =>
'b[1]val3');
$b[2] = array('b[2]key1' => 'b[2]val1', 'b[2]key2' => 'b[2]val2', 'b[2]key3' =>
'b[2]val3');

More like that. $a and $b are db results (arrays). Each element has its own
array of feild=>value.

I tried a couple things, nesting those scripts inside each other, but to no
avail.

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

am 11.09.2007 03:13:34 von Jerry Stuckle

J. Frank Parnell wrote:
> On Fri, 07 Sep 2007 21:34:22 -0400, Jerry Stuckle
> wrote:
>
>> Jerry Stuckle wrote:
>>> J. Frank Parnell wrote:
>>>> On Fri, 07 Sep 2007 15:01:24 -0400, Jerry Stuckle
>>>>
>>>> wrote:
>>>>
>>>>> Jerry Stuckle wrote:
>>>>>> J. Frank Parnell wrote:
>>>>>>> On Fri, 07 Sep 2007 07:22:46 -0400, Jerry Stuckle
>>>>>>>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> J. Frank Parnell wrote:
>>>>>>>>> On Thu, 06 Sep 2007 21:41:27 -0400, Jerry Stuckle
>>>>>>>>>
>>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>>> J. Frank Parnell wrote:
>>>>>>>>>>> Hello,
>>>>>> reset $array1;
>>>>>> reset $array2;
>>>>>>
>>>>>> for (($val1 = each($array1)) || ($val2 = each($array2)) {
>>>>>> if ($val1) { // false if at the end
>>>>>> // $val1['key'] contains the key
>>>>>> // $val1['value'] contains the value
>>>>>> echo $val1['key'] . '=>' $val1['value'];
>>>>>> }
>>>>>> else
>>>>>> echo ' ';
>>>>>> if ($val2) { // false if at the end
>>>>>> // $val2['key'] contains the key
>>>>>> // $val2['value'] contains the value
>>>>>> echo $val2['key'] . '=>' $val2['value'];
>>>>>> }
>>>>>> else
>>>>>> echo ' ';
>>>>>> }
>>>>>>
>>>>>>
>>>>> Sorry - that should be:
>>>>>
>>>>> for (($val1 = each($array1)) || ($val2 = each($array2))) {
>>>>>
>>>>> Missed an extra paren at the end.
>>>> Thanks again for the help, however, I still get:
>>>> Parse error: syntax error, unexpected ')', expecting ';' in
>>>> /web/sites/asdf/asdf.com/_impexp/test.php on line 55
>>>>
>>>> Isnt there supposed to be 3 expressions in a FOR? I tried with
>>>> semicolons here
>>>> and there, but couldnt get it.
>>>> current code:
>>>> for (($val1 = each($users)) || ($val2 = each($newusers))) {
>>>> if ($val1) { // false if at the end
>>>> // $val1['key'] contains the key
>>>> // $val1['value'] contains the value
>>>> echo $val1['key'] . '=>' $val1['value'];
>>>> }
>>>> else
>>>> echo ' ';
>>>> if ($val2) { // false if at the end
>>>> // $val2['key'] contains the key
>>>> // $val2['value'] contains the value
>>>> echo $val2['key'] . '=>' $val2['value'];
>>>> }
>>>> else
>>>> echo ' ';
>>>> }
>>>>
>>>>
>>>>
>>> Sorry - it should be a 'while' loop, not a 'for' loop.
>>>
>> Also, see the correction above in response to Rik's comments:
>
> OK, got it thanks. Everybody, Rik, Jerry, Gosha and Satya...I appreciate it.
>
> Now, I go to test things and of course, I forgot one big detail. The two arrays
> are actually multidimensional themselves:
> $a[0] = array('a[0]key1' => 'a[0]val1', 'a[0]key2' => 'a[0]val2');
> $a[1] = array('a[1]key1' => 'a[1]val1', 'a[1]key2' => 'a[1]val2');
> $b[0] = array('b[0]key1' => 'b[0]val1', 'b[0]key2' => 'b[0]val2', 'b[0]key3' =>
> 'b[0]val3');
> $b[1] = array('b[1]key1' => 'b[1]val1', 'b[1]key2' => 'b[1]val2', 'b[1]key3' =>
> 'b[1]val3');
> $b[2] = array('b[2]key1' => 'b[2]val1', 'b[2]key2' => 'b[2]val2', 'b[2]key3' =>
> 'b[2]val3');
>
> More like that. $a and $b are db results (arrays). Each element has its own
> array of feild=>value.
>
> I tried a couple things, nesting those scripts inside each other, but to no
> avail.
>

So, what is it exactly you want?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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

am 11.09.2007 04:45:44 von pos

On Mon, 10 Sep 2007 21:13:34 -0400, Jerry Stuckle
wrote:


>> Now, I go to test things and of course, I forgot one big detail. The two arrays
>> are actually multidimensional themselves:
>> $a[0] = array('a[0]key1' => 'a[0]val1', 'a[0]key2' => 'a[0]val2');
>> $a[1] = array('a[1]key1' => 'a[1]val1', 'a[1]key2' => 'a[1]val2');
>> $b[0] = array('b[0]key1' => 'b[0]val1', 'b[0]key2' => 'b[0]val2', 'b[0]key3' =>
>> 'b[0]val3');
>> $b[1] = array('b[1]key1' => 'b[1]val1', 'b[1]key2' => 'b[1]val2', 'b[1]key3' =>
>> 'b[1]val3');
>> $b[2] = array('b[2]key1' => 'b[2]val1', 'b[2]key2' => 'b[2]val2', 'b[2]key3' =>
>> 'b[2]val3');
>>
>> More like that. $a and $b are db results (arrays). Each element has its own
>> array of feild=>value.
>>
>> I tried a couple things, nesting those scripts inside each other, but to no
>> avail.
>>
>
>So, what is it exactly you want?

Same as before, go through each array, for each element, list all the
key=>values for both:
$a[0][key]-val -- $b[0][key]-val
// etc for all key-vals in $a[0] and $b[0]
$a[1][key]-val -- $b[1][key]-val
//etc

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

am 11.09.2007 17:00:07 von Jerry Stuckle

J. Frank Parnell wrote:
> On Mon, 10 Sep 2007 21:13:34 -0400, Jerry Stuckle
> wrote:
>
>
>>> Now, I go to test things and of course, I forgot one big detail. The two arrays
>>> are actually multidimensional themselves:
>>> $a[0] = array('a[0]key1' => 'a[0]val1', 'a[0]key2' => 'a[0]val2');
>>> $a[1] = array('a[1]key1' => 'a[1]val1', 'a[1]key2' => 'a[1]val2');
>>> $b[0] = array('b[0]key1' => 'b[0]val1', 'b[0]key2' => 'b[0]val2', 'b[0]key3' =>
>>> 'b[0]val3');
>>> $b[1] = array('b[1]key1' => 'b[1]val1', 'b[1]key2' => 'b[1]val2', 'b[1]key3' =>
>>> 'b[1]val3');
>>> $b[2] = array('b[2]key1' => 'b[2]val1', 'b[2]key2' => 'b[2]val2', 'b[2]key3' =>
>>> 'b[2]val3');
>>>
>>> More like that. $a and $b are db results (arrays). Each element has its own
>>> array of feild=>value.
>>>
>>> I tried a couple things, nesting those scripts inside each other, but to no
>>> avail.
>>>
>> So, what is it exactly you want?
>
> Same as before, go through each array, for each element, list all the
> key=>values for both:
> $a[0][key]-val -- $b[0][key]-val
> // etc for all key-vals in $a[0] and $b[0]
> $a[1][key]-val -- $b[1][key]-val
> //etc
>
>

OK, so just create an inner loop similar to the existing one, which goes
through each of the elements in the arrays.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================