我有一个 JS 数组,我将其转换为 JSON
JS
mycode[0][0]=true
mycode[0][1]="element1"
mycode[0][2]=400
mycode[0][3]=150
mycode[0][4]=148
mycode[0][5]=148
转成JSON:
[
[
true,
"element1",
400,
150,
148,
148
]
]
现在我将其推送到 PHP
PHP代码:
$decoded = json_decode($_GET["q"]);
$response=$q[0];
echo $response;
它输出一个字母或一个符号,因为 JSON 是一个字符串。
如果我使用 $decoded[0][0] 或 $decoded[0] 而不是 $q[0] 我什么也得不到...
我究竟做错了什么?
我想要什么?我需要在 JS 中使用相同的数组,只是在 PHP 中(该数组稍后将在 PHP 函数中使用)
最佳答案
代码:
<?php
$json = '[[ true,"element1",400,150,148,148 ]]';
$dec = json_decode($json);
var_dump($dec);
?>
输出:
array(1) {
[0]=>
array(6) {
[0]=>
bool(true)
[1]=>
string(8) "element1"
[2]=>
int(400)
[3]=>
int(150)
[4]=>
int(148)
[5]=>
int(148)
}
}
在这里工作正常。您的问题一定出在其他地方。
$ php -v
PHP 5.3.2 (cli) (built: Apr 27 2010 17:55:48)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
关于php - 如何从 JS 数组通过 JSON 获取 PHP 数组? (我至少在那里的一半),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2803557/