i need to convert a PHP $var to a array.

i did like this

$array = array();
$var=$array;

but this way the dosent give the output as this

Array ( [0] => [1] => 9 [2] => 7 [3] => 11 [4] => 5 )

its gives like a normal varible print 154515100

link|improve this question

i don't see how Array ( [0] => [1] => 9 [2] => 7 [3] => 11 [4] => 5 ) can be related to 154515100. show us the relevant code. – stillstanding Nov 21 '10 at 18:11
feedback

3 Answers

up vote 3 down vote accepted

It depends on what the variable is. If it is a string, do: $array = str_split($var);

link|improve this answer
this helped to solve the problem thanks ! – Sudantha Nov 21 '10 at 18:23
feedback

What is the variable to begin with?

If it was an object, you could cast it to an array like

$var=(array)$var;

It sounds like you want to convert from a string or an integer to an array, though. Please provide more details.

link|improve this answer
i have a string which is stored in a database and i need to convert it to a array in the frontend – Sudantha Nov 21 '10 at 18:15
in this way its set as Array ( [0] => 154515100 ) – Sudantha Nov 21 '10 at 18:19
Oh, okay. Yep, jbenet's answer is the way to go! – JAL Nov 21 '10 at 18:28
feedback

You should define $var as array first

link|improve this answer
feedback

Your Answer

 
or
required, but never shown
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.