How to assign a function number_format to all arrays?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ijekic
Forum Newbie
Posts: 11
Joined: Sat Aug 16, 2008 11:36 am
Location: Belgrade, Serbia

How to assign a function number_format to all arrays?

Post 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!
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: How to assign a function number_format to all arrays?

Post by Ziq »

Use a cycle
ssssss
Forum Newbie
Posts: 17
Joined: Fri Aug 29, 2008 8:34 am

Re: How to assign a function number_format to all arrays?

Post 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.
Ijekic
Forum Newbie
Posts: 11
Joined: Sat Aug 16, 2008 11:36 am
Location: Belgrade, Serbia

Re: How to assign a function number_format to all arrays?

Post 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.
ssssss
Forum Newbie
Posts: 17
Joined: Fri Aug 29, 2008 8:34 am

Re: How to assign a function number_format to all arrays?

Post 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.
Ijekic
Forum Newbie
Posts: 11
Joined: Sat Aug 16, 2008 11:36 am
Location: Belgrade, Serbia

Re: How to assign a function number_format to all arrays?

Post 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.
ssssss
Forum Newbie
Posts: 17
Joined: Fri Aug 29, 2008 8:34 am

Re: How to assign a function number_format to all arrays?

Post 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.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: How to assign a function number_format to all arrays?

Post 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, ',', ' ');
}
?>
 
Ijekic
Forum Newbie
Posts: 11
Joined: Sat Aug 16, 2008 11:36 am
Location: Belgrade, Serbia

Re: How to assign a function number_format to all arrays?

Post by Ijekic »

Thanks for help everyone!! It works!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to assign a function number_format to all arrays?

Post 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.
Ijekic
Forum Newbie
Posts: 11
Joined: Sat Aug 16, 2008 11:36 am
Location: Belgrade, Serbia

Re: How to assign a function number_format to all arrays?

Post by Ijekic »

Thanks onion!! That's also very helpful!
Post Reply