You are here: Re: using two arrays in a 'for each ..." statement « PHP Language « IT news, forums, messages
Re: using two arrays in a 'for each ..." statement

Posted by mantrid on 11/04/07 15:58

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,$nbyrsheldgroup,$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.googlegroups.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.
>

 

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

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