using two arrays in a 'for each ..." statement

This is a discussion on using two arrays in a 'for each ..." statement within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello I wish to extract two arrays and use them simultaneously in a for each statement Ive tried 'foreach ($yearsarray ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-03-2007
mantrid
 
Posts: n/a
Default using two arrays in a 'for each ..." statement

Hello
I wish to extract two arrays and use them simultaneously in a for each
statement
Ive tried
'foreach ($yearsarray as &$yearvalue, $percentarray as &$percentvalue) {'
and also
'foreach ($yearsarray as &$yearvalue AND $percentarray as &$percentvalue) {'

But neither work
my full code for the function is below. Can anyone help

****************************
Function taper_rel($aim,
$yearsheld,$byrsheldgroup,$bpercentgroup,$nbyrshel dgroup,$nbpercentgroup){
if($aim = 1){
$yearsarray = explode(",", $byrsheldgroup);
$percentarray = explode(",", $brpercentgroup);
}elseif($aim = 0){
$yearsarray = explode(",", $nbyrsheldgroup);
$percentarray = explode(",", $nbrpercentgroup);
}
foreach ($yearsarray as &$yearvalue, $percentarray as &$percentvalue) {
if ($yearsheld==$yearvalue){
return $percentvalue;
}
}

unset($yearvalue);
unset($percentvalue);
}
**************************
Thanks Ian


Reply With Quote
  #2 (permalink)  
Old 11-03-2007
C.
 
Posts: n/a
Default Re: using two arrays in a 'for each ..." statement

On 3 Nov, 15:15, "mantrid" <ian.dan...@virgin.net> wrote:
> Hello
> I wish to extract two arrays and use them simultaneously in a for each
> statement
> Ive tried
> 'foreach ($yearsarray as &$yearvalue, $percentarray as &$percentvalue) {'
> and also
> 'foreach ($yearsarray as &$yearvalue AND $percentarray as &$percentvalue) {'
>


If each entry in one array matches one in the other then:

foreach ($yeararray as $index=>$year) {
print $year, $percentarray[$index];
}

Even if these were not compound data structures (as indicated in the
remainder of your code) your data design is a mess; both values
should be held in the same record in the same array.

C.

Reply With Quote
  #3 (permalink)  
Old 11-04-2007
mantrid
 
Posts: n/a
Default Re: using two arrays in a 'for each ..." statement

I was thinking along these lines initially but didnt know much about arrays.
So thought the easiest way was to use two separate fields. However the two
fields match, in that the commar separated numbers in one field match the
commar separated numbers in the other.
Ive used
$percentarray = explode(",", $brpercentgroup);
to create two separate keyed arrays for each field.

If I combined the fields (easy at this stage as the table has only recently
been created) how would I extract the field contents into an associative
array, as I presume that is what will be needed.
How would I modify it to create an associative array?
would it involve using the '=>' in the glue parameter somehow?

ie field contains 0,100,3,90,4,80,5,85,9,70
into an associative 0=>100,3=>90,4=>80,5=>85,9=>70
If the array was called $percentarray, I presume I would then use;

foreach ($percentarray as $yearvalue=>$percentvalue) {

to extract the two values to use in the function below?


*************
Function taper_rel($aim,
$yearsheld,$byrsheldgroup,$bpercentgroup,$nbyrshel dgroup,$nbpercentgroup){
if($aim = 1){
$yearsarray = explode(",", $byrsheldgroup);
$percentarray = explode(",", $brpercentgroup);
}elseif($aim = 0){
$yearsarray = explode(",", $nbyrsheldgroup);
$percentarray = explode(",", $nbrpercentgroup);
}
foreach ($yearsarray as &$yearvalue, $percentarray as &$percentvalue) {
if ($yearsheld==$yearvalue){
return $percentvalue;
}
}

unset($yearvalue);
unset($percentvalue);
}
***********************
Ian


"C." <colin.mckinnon@gmail.com> wrote in message
news:1194110709.612682.161550@19g2000hsx.googlegro ups.com...
> On 3 Nov, 15:15, "mantrid" <ian.dan...@virgin.net> wrote:
> > Hello
> > I wish to extract two arrays and use them simultaneously in a for each
> > statement
> > Ive tried
> > 'foreach ($yearsarray as &$yearvalue, $percentarray as &$percentvalue)

{'
> > and also
> > 'foreach ($yearsarray as &$yearvalue AND $percentarray as

&$percentvalue) {'
> >

>
> If each entry in one array matches one in the other then:
>
> foreach ($yeararray as $index=>$year) {
> print $year, $percentarray[$index];
> }
>
> Even if these were not compound data structures (as indicated in the
> remainder of your code) your data design is a mess; both values
> should be held in the same record in the same array.
>
> C.
>




Reply With Quote
  #4 (permalink)  
Old 12-20-2007
My Pet Programmer
 
Posts: n/a
Default Re: using two arrays in a 'for each ..." statement

If your arrays are in parallel, the you don't have to do anything fancy
with the language construct. If I had two arrays that were parallel like
that (having corresponding values in the same positions), I would
combine them like so:

$array1 ("apple", "orange", "banana");
$array2 ("pie", "juice", "smoothy");

$array3 = array();
for ($i = 0; $i < count($array1); ++$i) {
$array3[$array1[$i]] = $array2[$i];
}


And then you could use the

foreach ($array3 as $fruit=>$tastyTrest) {}


Or call on them as $array3["apple"];, etc

~A!

mantrid took the time to say:
> I was thinking along these lines initially but didnt know much about arrays.
> So thought the easiest way was to use two separate fields. However the two
> fields match, in that the commar separated numbers in one field match the
> commar separated numbers in the other.
> Ive used
> $percentarray = explode(",", $brpercentgroup);
> to create two separate keyed arrays for each field.
>
> If I combined the fields (easy at this stage as the table has only recently
> been created) how would I extract the field contents into an associative
> array, as I presume that is what will be needed.
> How would I modify it to create an associative array?
> would it involve using the '=>' in the glue parameter somehow?
>
> ie field contains 0,100,3,90,4,80,5,85,9,70
> into an associative 0=>100,3=>90,4=>80,5=>85,9=>70
> If the array was called $percentarray, I presume I would then use;
>
> foreach ($percentarray as $yearvalue=>$percentvalue) {
>
> to extract the two values to use in the function below?
>
>
> *************
> Function taper_rel($aim,
> $yearsheld,$byrsheldgroup,$bpercentgroup,$nbyrshel dgroup,$nbpercentgroup){
> if($aim = 1){
> $yearsarray = explode(",", $byrsheldgroup);
> $percentarray = explode(",", $brpercentgroup);
> }elseif($aim = 0){
> $yearsarray = explode(",", $nbyrsheldgroup);
> $percentarray = explode(",", $nbrpercentgroup);
> }
> foreach ($yearsarray as &$yearvalue, $percentarray as &$percentvalue) {
> if ($yearsheld==$yearvalue){
> return $percentvalue;
> }
> }
>
> unset($yearvalue);
> unset($percentvalue);
> }
> ***********************
> Ian
>
>
> "C." <colin.mckinnon@gmail.com> wrote in message
> news:1194110709.612682.161550@19g2000hsx.googlegro ups.com...
>> On 3 Nov, 15:15, "mantrid" <ian.dan...@virgin.net> wrote:
>>> Hello
>>> I wish to extract two arrays and use them simultaneously in a for each
>>> statement
>>> Ive tried
>>> 'foreach ($yearsarray as &$yearvalue, $percentarray as &$percentvalue)

> {'
>>> and also
>>> 'foreach ($yearsarray as &$yearvalue AND $percentarray as

> &$percentvalue) {'
>> If each entry in one array matches one in the other then:
>>
>> foreach ($yeararray as $index=>$year) {
>> print $year, $percentarray[$index];
>> }
>>
>> Even if these were not compound data structures (as indicated in the
>> remainder of your code) your data design is a mess; both values
>> should be held in the same record in the same array.
>>
>> C.
>>

>
>
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:11 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0