0

I have a string like '6554', and I would like to change this to an array, resembling the following:

Array([0] => 6, [1] => 5, [2] => 5, [3] => 4)

The problem is that I can not be certain of the length of the number and the number has nothing separating each digit. The number will not be negative or a float. Any ideas on how I could do this?

q3d
  • 3,473
  • 8
  • 34
  • 39
  • 2
    What's the matter with `str_split()`? – Maerlyn Jan 04 '12 at 11:18
  • 1
    possible duplicate of [PHP: Split string into array, like explode with no delimiter](http://stackoverflow.com/questions/2170320/php-split-string-into-array-like-explode-with-no-delimiter) -- please use the search before you ask a new question. – Felix Kling Jan 04 '12 at 11:19

2 Answers2

2

How about using preg_split

$data=preg_split('//', $number, -1, PREG_SPLIT_NO_EMPTY);
print_r($data);
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

New way to do this

$str = '123456';
for($i=0 ; $i < strlen($str) ;$i++)
{
   $array[]=$str{$i};
}
Gaurav
  • 28,447
  • 8
  • 50
  • 80