我在从 php 调用 javascript 变量时遇到问题json_encode()。在 javascript 中,我无法获得 array.length 因为有标签1:。我想要[res_tmp]数组获取像[sample]数组一样的格式(没有标签数字数组,例如:1:,2:)。感谢您的帮助。
回声(假):
res_tmp: {
0: null,
1: {
name: "AAA",
sort: 1
},
2: {
name: "DDD",
sort: 2
},
...
回声(我希望):
res_tmp: {
null,
{
name: "AAA",
sort: 1
},
{
name: "DDD",
sort: 2
},
...
这是我的代码:
$tmp = [
[
"name" => "1.AAA",
"sort" => 1,
],[
"name" => "10.CCC",
"sort" => 10,
],[
"name" => "11.BBB",
"sort" => 11,
],[
"name" => "2.DDD",
"sort" => 2,
],[
"name" => "3.EEE",
"sort" => 3,
],
];
for ($i=0; $i < count($tmp); $i++) {
$res_tmp[ $tmp[$i]['sort'] ] = $tmp[$i];
}
$nul = [0,4,5,6,7,8,9,12];
for ($j=0; $j < count($nul); $j++) {
$res_tmp[ $nul[$j] ] = null;
}
$val = [
"sample" => $tmp,
"res_tmp" => $res_tmp,
];
echo json_encode($val);
// echo "<pre>",print_r($val),"</pre>";
largeQ