q3d
q3d

Reputation: 3523

Number string to array

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?

Upvotes: 0

Views: 167

Answers (2)

Gaurav
Gaurav

Reputation: 28755

New way to do this

$str = '123456';
for($i=0 ; $i < strlen($str) ;$i++)
{
   $array[]=$str{$i};
}

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86336

How about using preg_split

$data=preg_split('//', $number, -1, PREG_SPLIT_NO_EMPTY);
print_r($data);

Upvotes: 2

Related Questions