lindm Posted November 10, 2008 Share Posted November 10, 2008 Is it possible to number format an array? Say I have an array in the format 99999999. I want to add thousand delimiter with a space like this function: number_format($string, 0, ',', ' '); Possible? Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/ Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687070 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 Wait, how is your array formatted? Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687103 Share on other sites More sharing options...
lindm Posted November 10, 2008 Author Share Posted November 10, 2008 Array is formatted without any whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687109 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 That makes no sense. Do you know what an array is? Do you possibly mean you have a string? Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687110 Share on other sites More sharing options...
lindm Posted November 10, 2008 Author Share Posted November 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687115 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 Oh, so you have an array with all of the numbers like this? $array[] = 99999999; $array[] = 28931; $array[] = 21389183981; Etc. Correct? Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687120 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687122 Share on other sites More sharing options...
wildteen88 Posted November 10, 2008 Share Posted November 10, 2008 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, ',', ' '); } Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687126 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 @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. Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687130 Share on other sites More sharing options...
wildteen88 Posted November 10, 2008 Share Posted November 10, 2008 @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. Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687132 Share on other sites More sharing options...
lindm Posted November 10, 2008 Author Share Posted November 10, 2008 Trying the foreach but stuck: My array is stored in $calc. It is then reformatted as json. This doesn't work: foreach ($calc as $key => $val) { $calc[$key] = number_format($val, 0, ',', ' '); } echo json_encode($calc); Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687169 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 The code you just posted should work as expected. Do: die(print_r($calc)); Before the json_encode() call. Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687174 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 Read up on json_encode, the post was incorrect, thus removed. Sorry bout that. Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687177 Share on other sites More sharing options...
wildteen88 Posted November 10, 2008 Share Posted November 10, 2008 That code should work fine. This is the test data I used $calc[] = 99999999; $calc[] = 28931; $calc[] = 21389183981; The output of your script is ["99 999 999","28 931","21 389 183 981"] Nothing wrong there Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687178 Share on other sites More sharing options...
lindm Posted November 11, 2008 Author Share Posted November 11, 2008 Got it! Was an error elsewhere. Thanks all! Quote Link to comment https://forums.phpfreaks.com/topic/132195-solved-number-format-array/#findComment-687932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.