ssssss wrote:I'm assuming that when you say "ALL arrays", you mean "All the values in my array". You have only shown us a single array in your code sample. And, there are other arrays like $_REQUEST, $_SERVER, $_SESSION that you surely do not want to apply a number format to.
So, if you want to do something to every value in an array you are using, you have three choices:
1. format the values before or as you put them in the array
2. loop through the values in the array and apply the format to them
3. apply the format as you use the values
If you tried a loop and couldn't get it to work, post your code and I'll take a look.
Hey thanks!
Yes, I meant all values in one array.
Code: Select all
$val1 = $_POST['val1'];
$val2 = $_POST['val2'];
$total = $_POST['total'];
$euro = 99.9;
$dollar = 44.4;
$calc = array(
'dt' => ($dollar * $total),
'et' => ($euro * $total),
'td' => ($total / $dollar),
'te' => ($total / $euro));
if ($val1 == 'EUR' && $val2 == 'US') {
echo {$calc['td']};
}
else { {$calc['te']};
}
I know I could use, for example,
Code: Select all
number_format (($dollar * $total),2);
on every single key, but I wanted it to make more simple and write just one number_format variable that would be applied to all array keys everytime I call a certain key.
I tried with:
Code: Select all
$calc = number_format (array(
'dt' => ($dollar * $total),
'et' => ($euro * $total),
'td' => ($total / $dollar),
'te' => ($total / $euro)),2);
but couldn't get the caluclation.
I also tried with
Code: Select all
foreach ($calc as $key => $value) {
number_format ($value,2, ',', ' ');}
... but that displayed the numbers for all keys, not, for example, $calc['td'] only.