Insane Skull Insane Skull - 7 months ago 15
Javascript Question

How to Convert string array into simple array?

I know that, this question was asked too many times, but it was an different case.

I have already tried too many ways, but doesn't found any workaround.

$array = ["9","8","7","6","5"]; //javascript JSON.stringify() output

json_decode($array); //expects parameter 1 to be string, array given
explode(',', $array); // Array ( [0] => ["7" [1] => "6"] )


I had also tried with
array_shift()
,
array_walk()
,
map()
but still no luck.

output

array(
[0] => 9
[1] => 8
[2] => 7
[3] => 6
[4] => 5
)

Answer

Not 100% clear, but if you firstly get your JSON as string, maybe you'll get what you want?

<?php

$array = <<<END
["9","8","7","6","5"]
END;

$php_array = json_decode($array);

$string = join(',', $php_array);
$output = explode(',', $string);
Comments