Jump to content

[SOLVED] Number format array?


lindm

Recommended Posts

I would hope so....

 

 

http://us.php.net/manual/en/function.number-format.php

 

 

It is a function of PHP.

 

// Using format string number_format  ( float $number  , int $decimals  , string $dec_point  , string $thousands_sep  )
$string = 9999999999;
$formattedNumber = number_format($string, 0, '.', ', '); 

 

But I think your parameters are in the wrong spot, try the above.

Well hope I know what an array is...stored values...

 

What I am looking for is to reformat the whole array in one go.

 

Old approach to change number format for single value:

 

number_format($arrayvalue["xxx"], 0, ',', ' ');

 

Say array is stored in $array.

 

Trying the approach number_format($array, 0, ',', ' '); but no go.

<?php
foreach ($arrayvalue as $key => $val) {
      $arrayvalue[$key] = number_format($val, 0, ',', ' ');
}

print_r($arrayvalue); // print out the array for verification
?>

 

Sorry misunderstood the first part. Hope that helps.

 

You were looking for www.php.net/foreach

If you array is a single dimension and it only contains numbers -- which you want to format then use array_walk

 

eg

 

$numbers = array(1, 12.06593, 1253689, .0669);

echo '<pre>' . print_r($numbers, true) . '</pre>';

array_walk($numbers, 'format_number_array');

echo '<p>FORMATTED:<pre>' . print_r($numbers, true) . '</pre>';

function format_number_array(&$item)
{
    $item = number_format($item, 0, ',', ' ');
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.