我问的是转换字符串公式的最佳方法,如JSON输入[" num1"," num2"," num3",...]到PHP 7和JS中的数组 数组应该是那样的
$tobeconverted= "[\"1\",\"2\",\"3\"]" ; // consider escape character so ["1","2","3"]
$number= array("1", "2", "3");
答案 0 :(得分:2)
" JS数据"之间的转换(JSON)和PHP可以使用json_encode和json_decode
完成<?php
$number= array("1", "2", "3");
$json = json_encode($number);
echo $json;
# ["1","2","3"]
$array = json_decode($json);
var_dump($array);
# array(3) {
# [0]=>
# string(1) "1"
# [1]=>
# string(1) "2"
# [2]=>
# string(1) "3"
# }
答案 1 :(得分:0)
var x=[32,31,30,29];
var y=JSON.stringify(x);
document.getElementById("hidden").value=y;
<input type="hidden" id="hidden">
PHP
$a=json_decode($_POST['hidden'],1);
var_dump($a);
答案 2 :(得分:0)
这是我找到的解决方案。希望它对你有所帮助。
PHP:
<?php
$tobeconverted= "[\"1\",\"2\",\"3\"]";
$arraystring = substr($tobeconverted, 1, strlen($tobeconverted) - 2);
$array = explode(",", $arraystring);
foreach ($array as $item) {
echo $item . '<br/>';
}
?>
JS:
var tobeConverted = "[\"3\",\"4\",\"5\"]";
var array = JSON.parse(tobeConverted);