Page 1 of 1
How to assign a function number_format to all arrays?
Posted: Fri Sep 12, 2008 8:12 am
by Ijekic
This is probably very simple question, I searched some functions in PHP manual but couldn't find the answer.
I have an array full of simple calculations, like this one:
Code: Select all
$calc = array(
'dt' => ($dollar * $total),
'et' => ($euro * $total),
'td' => ($total / $dollar))
I'd like to format the result number with number_format. I just don't want to repeat number_format to every single array key.
I tried with $calc = number_format ($calc,2), but without any luck - everytime I called an array key (e.g. $calc['dt']), I received "1".
Thanks!
Re: How to assign a function number_format to all arrays?
Posted: Fri Sep 12, 2008 8:33 am
by Ziq
Use a cycle
Re: How to assign a function number_format to all arrays?
Posted: Fri Sep 12, 2008 8:44 am
by ssssss
I think that "loop" is the more common english term for what Ziq calls a "cycle". Unless I'm mistaken, in which case I apologize for second guessing you.
You can also use the array_walk() function if you want something built in. It just loops through the array applying a function that you write. A simple loop is probably easier to read and understand though.
Re: How to assign a function number_format to all arrays?
Posted: Fri Sep 12, 2008 5:55 pm
by Ijekic
Thanks, but I still didn't make it.
You see, I need to apply the number_format function to ALL arrays, but I want to pick only one array key at a time and I need to have that key already formatted. I hope it's clearer now. I tried foreach loop function, but that didn't work.
Re: How to assign a function number_format to all arrays?
Posted: Fri Sep 12, 2008 6:51 pm
by ssssss
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.
Re: How to assign a function number_format to all arrays?
Posted: Sat Sep 13, 2008 5:01 pm
by Ijekic
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.
Re: How to assign a function number_format to all arrays?
Posted: Sat Sep 13, 2008 5:56 pm
by ssssss
Your last code sample shouldn't have displayed anything. It was, though, very close to what you need.
number_format() doesn't change the value of the parameter you pass it. It returns the formatted string, and leaves the parameter value alone. So you want to reset the values in your array in the loop. Like this:
Code: Select all
foreach ($calc as $key => $value) {
$calc[$key] = number_format($value, 2, ',' , ' ');
}
You can do that right after you create the array.
Personally, I would just do the formatting as you create the array. It saves you a tiny amount of processing. But it's not a big deal. This way is fine too.
Re: How to assign a function number_format to all arrays?
Posted: Sat Sep 13, 2008 6:01 pm
by Ziq
Code: Select all
foreach ($calc as $key => $value) {
number_format ($value,2, ',', ' ');}
You have committed an error in your php-code. This loop not change any data in array. For example, try use this code:
Code: Select all
<?
foreach ($calc as $key => $value)
{
$calc[$key] = number_format ($value,2, ',', ' ');
}
?>
Re: How to assign a function number_format to all arrays?
Posted: Mon Sep 15, 2008 4:58 am
by Ijekic
Thanks for help everyone!! It works!
Re: How to assign a function number_format to all arrays?
Posted: Mon Sep 15, 2008 5:54 am
by onion2k
Just for the record, attaching a function to a variable (or an array, or an associative array, or whatever) is called 'tieing', but PHP can't do it. Other languages, eg Perl, can. If you want to achieve the same effect create an object and use that in place of the variable, with magic functions* for the calculation, eg:
Code: Select all
class Thing() {
public $value;
public function __set($value) {
$this->value = $value;
}
public function __get() {
return $value*2;
}
}
$var = new Thing;
$var->value = 2;
echo $var->value; // Output is 4
Obviously that's a bit of overkill for something trivial like doubling a number, but they can be incredibly useful for more complex things. They're only available in PHP 5.
* Yes, they really are called magic functions.
Re: How to assign a function number_format to all arrays?
Posted: Mon Sep 22, 2008 8:28 pm
by Ijekic
Thanks onion!! That's also very helpful!