tags:

views:

74

answers:

3

Ok I know this is simple, in fact damn simple, I did this in the last project about a week ago, but I don't know why it has skipped off my head.

Input:

$word = "stack";

What I want to do is:

$array = array("s", "t", "a", "c", "k");

Any help will be appreciated!

+1  A: 

str_split();

Ionuț G. Stan
I feel so stupid..DAMNThanks for fast reply, needed urgently!
born2hack
Don't feel bad. It can be hard to find these PHP functions and there are a lot of them, more than most people know.
cletus
A: 

var_dump(str_split("abc"));

Omry
+2  A: 

Your question isn't too clear, but I suspect you want to turn your string into an array. You don't have to, actually. You can just access $word as if it were an array:

$word = 'stack';
for ($i=0, $m = strlen($word); $i < $m; $i++)
    echo $word[$i], "\n";
MathieuK
What if he wants to use `foreach` with that string? In PHP, strings are half-arrays, unfortunately.
Ionuț G. Stan
Well, that'd be an odd way of going about things (I want to loop over this string, but it HAS to be foreach even though a for-loop would do). But, indeed, there are cases where you are better off using str_split(). But if at all possible I'd favor not using str_split() as it creates a new array and `strlen($word)` new strings.
MathieuK
A `foreach` is certainly more readable than a `for` loop.
Ionuț G. Stan
I guess I figured it out why certain PHP aspects are so bad. Because people don't care about them.
Ionuț G. Stan