Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this string:

$string = "123456789"

It should become an array:

Array ( [0] => 123 ) Array ( [1] => 456 ) Array ( [2] => 789 )

but I get:

Array ( [0] => 123 ) Array ( [0] => 456 ) Array ( [0] => 789 )

I use loop to get this string:

for ($i=0; $i<$num; $i++)
{
    $string = $tree['a']['b'][$i]['c'];
}

Next I use explode function to get an array. Why I got the array with only one index - zero? What I should do to get:

 Array ( [0] => 123 ) Array ( [1] => 456 ) Array ( [2] => 789 ).

Please help me understand this.

share|improve this question
What generates the output you publish here? What is $tree? What is ` $string` ? – hakre Apr 24 '12 at 6:44
I parse xml file. I got data in a tree structure (multi-dimensional Array). – Delicja Apr 24 '12 at 6:51

1 Answer

Use str_split($string, 3) to create the array.

http://php.net/manual/en/function.str-split.php

share|improve this answer
Thanks but I got the same (array with only one index). – Delicja Apr 24 '12 at 7:03
the function str_split should work $arr = str_split($string, 3); – SuperNoob Apr 24 '12 at 7:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.