0

i have an array with numbers

print_r($arr_usr_id[$key]); 

output: "930933934"

now i need to convert this to 930,933,934 as a string with commas...

$newnumbers = (chunk_split($arr_usr_id[$key],3,",")); 

works but output: "930,933,934," gives me a comma after the last number

with str_split($arr_usr_id[$key], 3); i get three "Array" as output...

what is the best way to sepperate the numbers (from: 123145124984 to: 123,145,124,984,...) with commas?

0

3 Answers 3

3

Try to use str_split() with implode():

$newnumbers = implode(',', str_split($arr_usr_id[$key], 3));

<?php
header('Content-Type: text/plain');

$test   = '123456789';
$result = implode(',', str_split($test, 3));

var_dump($result);
?>

Shows:

string(11) "123,456,789"
Sign up to request clarification or add additional context in comments.

7 Comments

with this i get "930933934" :/
@Jim on my side it is string(11) "930,933,934". Check your syntax.
$numbers = $arr_usr_id[$key]; var_dump($numbers); output: string(3) "930" string(3) "933" string(3) "934" $newnumbers = implode(',', str_split($numbers, 3)); var_dump($newnumbers); output: string(3) "930" string(3) "933" string(3) "934"
if i enter manually numbers $numbers = "465444333"; this should work...it seems to be a problem with my array your method should work
@Jim try to check database encoding and php encoding to be compatible.
|
2

use:

number_format( $arr_usr_id[$key] );

[EDIT]

Documentation : number_format

1 Comment

Documentation of which is available here.
2
implode(',', str_split($arr_usr_id[$key], 3));

or

substr(chunk_split($arr_usr_id[$key],3,","), 0, -1);

Perhaps? :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.