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.
The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string.
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.
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.
$array = str_split($int)
and $num_digits = strlen($int)
should work just fine.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With