views:

1693

answers:

5

I have a string such as:

"0123456789"

and need to split EACH character into an array.

I for the hell of it tried:

explode('', '123545789');

But it gave me the obvious: Warning: No delimiter defined in explode) ..

How would I come across this? I can't see any method off hand, especially just a function

+5  A: 
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");

str_split also takes a 2nd param, the chunk length, so you can do things like:

$array = str_split("aabbccdd",2);

// $array[0] = aa
// $array[1] = bb
// $array[2] = cc  etc ...

You can also get at parts of your string by treating it as an array:

$string = "hello";
echo $string[1];

// outputs "e"
Erik
Ah. Missed that function, was splitting binary numbers to becalculated in an array, this works well.
oni-kun
+3  A: 

What are you trying to accomplish? You can access characters in a string just like an array

$s = 'abcd';
echo $s[0];

prints 'a'

nategood
A: 

str_split can do the trick. Note that strings in PHP can be accessed just like a chars array, in most cases, you won't need to split your string into a "new" array.

Soufiane Hassou
+1  A: 

Try this:

<?php
$str = '123456789';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
?>
Conan
That's really unnecessary, and quite a bit slower then str_split.
Erik
A: 

@Erik
Don't you mean...?

$array = str_split("0123456789");
rrrfusco
Erik