 |
|
 |
|
Next: what does this error message mean?
|
| Author |
Message |
External

Since: Jul 31, 2007 Posts: 16
|
(Msg. 1) Posted: Thu Sep 06, 2007 5:52 pm
Post subject: can you foreach() two arrays at once ? Archived from groups: comp>lang>php (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Aug 11, 2004 Posts: 3780
|
(Msg. 2) Posted: Thu Sep 06, 2007 9:41 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
J. Frank Parnell wrote:
> Hello,
> So, I was wondering how to do this:
>
> foreach($foo as $k=>$v AND $bar as $k2=>$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) = 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.DeleteThis@attglobal.net
================== >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Jul 31, 2007 Posts: 16
|
(Msg. 3) Posted: Thu Sep 06, 2007 9:41 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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? >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Aug 11, 2004 Posts: 3780
|
(Msg. 4) Posted: Fri Sep 07, 2007 7:22 am
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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 RemoveThis @attglobal.net
================== >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Aug 31, 2007 Posts: 6
|
(Msg. 5) Posted: Fri Sep 07, 2007 7:28 am
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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/ >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Aug 31, 2007 Posts: 6
|
(Msg. 6) Posted: Fri Sep 07, 2007 7:29 am
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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". >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Jul 31, 2007 Posts: 16
|
(Msg. 7) Posted: Fri Sep 07, 2007 8:57 am
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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. >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Aug 11, 2004 Posts: 3780
|
(Msg. 8) Posted: Fri Sep 07, 2007 1:13 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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.TakeThisOut@attglobal.net
================== >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Aug 11, 2004 Posts: 3780
|
(Msg. 9) Posted: Fri Sep 07, 2007 3:01 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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.TakeThisOut@attglobal.net
================== >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Jul 31, 2007 Posts: 16
|
(Msg. 10) Posted: Fri Sep 07, 2007 3:01 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 ' ';
} >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Aug 11, 2004 Posts: 3780
|
(Msg. 11) Posted: Fri Sep 07, 2007 6:17 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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.TakeThisOut@attglobal.net
================== >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 568
|
(Msg. 12) Posted: Fri Sep 07, 2007 6:47 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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...
--
Rik Wasmus >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Jul 31, 2007 Posts: 16
|
(Msg. 13) Posted: Fri Sep 07, 2007 6:47 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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 >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 568
|
(Msg. 14) Posted: Fri Sep 07, 2007 7:56 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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?
--
Rik Wasmus >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
External

Since: Aug 11, 2004 Posts: 3780
|
(Msg. 15) Posted: Fri Sep 07, 2007 7:56 pm
Post subject: Re: can you foreach() two arrays at once ? [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 '<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) = 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 DeleteThis @attglobal.net
================== >> Stay informed about: can you foreach() two arrays at once ? |
|
| Back to top |
|
 |  |
| Related Topics: | Foreach Loop with Nested Arrays - Hey Y'all, I haven't programmed in PHP in quite a while. Could anyone help me with a foreach loop in a nested array? I have a function which returns $stock (for an online pricing catalog). $stock[0] is ( [0] => Item, [1] => Sale, ...) an array ...
Problem With Arrays Of Arrays, PHP 5.2.0, Windows - $aThePosts = array_change_key_case($_POST, CASE_LOWER); define("CONTACT_IS_LOCAL", 0); define("CONTACT_IS_REMOTE", 1); /* $aWho contains an array of arrays (contact details) * array( * array(0, CONTACT_IS_LOCAL, "My Local Con...
Indexing non-arrays as arrays, no warnings? - I was a bit surprised to find that indexing some non-arrays as if they were arrays does not even raise a Notice message. This works for NULL, integer, and floats at least. (PHP-5.2.1) error_reporting(E_ALL+E_STRICT); $arry = array(1=>1); $nullv...
foreach and str_replace - So I've been trying to get a bit of code to: Read all of the files in a dir called thumbs, but not the . and .., use the filename in a link to get the same filename in an images dir. Now I'm trying to use a foreach and glob as suggested, and get rid of...
Variable Array Name in foreach - Hi, I'm trying to use nested foreach loops to loop through two arrays. Each element in the first array corresponds to another array. The brandcode part of the first array is the same as the name of the corresponding array. Basically, one array is of the.... |
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
 |
|
|