Crazydog
06-30-2007, 01:23 AM
I have a foreach loop, and it needs to output different variables for each array value.
i.e.
array has the keys one, two, three
I need the foreach to output variables like:
$onefinal $twofinal $threefinal.
How would I include the key in the variable name?
EDIT: Solved my problem!
I changed the variable I needed to be unique to
${$value}
that way, each of the values of the array would be its own unique variable
here's a snippit:
$sums[one] = "Q1";
$sums[two] = "Q2";
//etc..
foreach($sums as $key => $value){
$sumq = "SELECT SUM($value) FROM results";
$sumr = mysql_query($sumq);
$arr = mysql_fetch_row($sumr);
$finalvalue = $arr[0];
$avg = $finalvalue / $num;
${$value} = round($avg, 2);
}
that gave me unique variables named $Q1, $Q2, etc.
i.e.
array has the keys one, two, three
I need the foreach to output variables like:
$onefinal $twofinal $threefinal.
How would I include the key in the variable name?
EDIT: Solved my problem!
I changed the variable I needed to be unique to
${$value}
that way, each of the values of the array would be its own unique variable
here's a snippit:
$sums[one] = "Q1";
$sums[two] = "Q2";
//etc..
foreach($sums as $key => $value){
$sumq = "SELECT SUM($value) FROM results";
$sumr = mysql_query($sumq);
$arr = mysql_fetch_row($sumr);
$finalvalue = $arr[0];
$avg = $finalvalue / $num;
${$value} = round($avg, 2);
}
that gave me unique variables named $Q1, $Q2, etc.