I send this Javascript array into PHP page using form submit {"1":"2","2":"2","3":"2","4":"2"}
Now, I want to convert this array into PHP array, like this
$cars = array("Volvo", "BMW", "Toyota");
So, this is what I tried:
$phparray = str_replace(':', ',', $_POST["questionandanswers"]); // Remove : and replace it with ,
$phparray = str_replace('}', '', $phparray); // Remove }
$phparray = str_replace('{', '', $phparray); // Remove {
echo '<br/>';
echo $phparray; // Output of this is: "1","2","2","2","3","2","4","2"
$questionandanswers = array($phparray); // Now convert it into PHP array
But it is not working. Looks like i cannot put $phparray
variable here array($phparray)
But, If instead of putting $phparray
variable in array($phparray)
, If i put the output of $phparray
manually, then, it works like: array("1","2","2","2","3","2","4","2")
What's the solution?