Jump to content

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, ',', ' ');
}

 

@wildteen: array_walk() returns a bool indicating success, not the new array.  That's why you pass it in by reference.  array_map() returns the new array, but doesn't take the element by reference.

Yeah had a typo fixed it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.