Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I explode an integer

Tags:

the answer to this could be easy. But I'm very fresh to programming. So be gentle...

I'm at work trying to do a quick fix for one of your customers. I want to get the total numbers of digits in a integer, and then explode the integer:

rx_freq = 1331000000 ( = 10 )   $array[0] = 1   $array[1] = 3   .   .   $array[9] = 0  rx_freq = 990909099 ( = 9 )   $array[0] = 9   $array[1] = 9   .   .   $array[8] = 9 

I'm not able to use explode, as this function need a delimiter. I've searched the eyh'old Google and Stackoverflow.

Basically: How do I explode an integer without delimiter, and how do I find the number of digits in an integer.

like image 304
chriscandy Avatar asked Feb 01 '11 22:02

chriscandy


People also ask

What does the explode () function do?

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string.

Can you split an integer in Python?

To split an integer into digits:Use the str() class to convert the integer to a string. Use a list comprehension to iterate over the string. On each iteration, use the int() class to convert each substring to an integer.

How do you split an integer into an array?

To split a number into an array:Convert the number to a string. Call the split() method on the string to get an array of strings. Call the map() method on the array to convert each string to a number.


2 Answers

$array = str_split($int) and $num_digits = strlen($int) should work just fine.

like image 114
mfonda Avatar answered Sep 21 '22 20:09

mfonda


Use the str_split() function:

$array = str_split(1331000000); 

Thanks to PHP's automated type coercion the passed int will be converted to a string automatically. But if you want you can also add an explicit cast.

like image 36
ThiefMaster Avatar answered Sep 25 '22 20:09

ThiefMaster