Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用json_encode()并仍在对象中发送数组_Javascript_Php_Arrays_Json - Fatal编程技术网

Javascript 使用json_encode()并仍在对象中发送数组

Javascript 使用json_encode()并仍在对象中发送数组,javascript,php,arrays,json,Javascript,Php,Arrays,Json,我发送了一个带有ID的AJAX请求,我希望PHP能给出一个带有附加信息的响应。PHP的响应如下: Array ( [success] => 1 [id] => 20 [fullname] => John Doe [statuslist] => Array ( [1] => Status1 [2] => Status2 [3] => St

我发送了一个带有ID的AJAX请求,我希望PHP能给出一个带有附加信息的响应。PHP的响应如下:

Array
(
    [success] => 1
    [id] => 20
    [fullname] => John Doe
    [statuslist] => Array
        (
            [1] => Status1
            [2] => Status2
            [3] => Status3
        )

)
{"success":true,"id":"20","fullname":"John Doe","statuslist":{"1":"Status1","2":"Status2","3":"Status3"}}
当我
json\u encode()时

return json_encode($response);
我得到的回应如下:

Array
(
    [success] => 1
    [id] => 20
    [fullname] => John Doe
    [statuslist] => Array
        (
            [1] => Status1
            [2] => Status2
            [3] => Status3
        )

)
{"success":true,"id":"20","fullname":"John Doe","statuslist":{"1":"Status1","2":"Status2","3":"Status3"}}
在JS中,当I
JSON.parse()
时,它将成为一个对象。但是
statuslist
也变成了嵌套对象,这就是问题所在

let res = JSON.parse(data) /* where data is response from server */
当我
console.log(res.statuslist)
时,我得到以下对象:

{ 1 : "Status1", 2 : "Status2", 3 : "Status3"}
我真正想要的是得到一个像我已经拥有的一样的对象,但是statuslist是一个2D数组,在这里我将在
console.log(res.statuslist)
上获得输出:

我的PHP数组如下所示:

$statusList = ArrayHelper::map(\app\models\SomeModel::find()->all(), 'id', 'title');

$response = [
    'success' => true,
    'id' => $id,
    'fullname' => $user->fullname,
    'statuslist' => $statusList,
];

问题来自您的初始数据:

Array
(
    [success] => 1
    [id] => 20
    [fullname] => John Doe
    [statuslist] => Array
        (
            [1] => Status1
            [2] => Status2
            [3] => Status3
        )

)
由于
statuslist
索引以
1
开始,而不是
0
json_encode()
不认为它是一个数组并将其编码为一个对象:

{ 1 : "Status1", 2 : "Status2", 3 : "Status3"}

用索引0启动PHP数组,它应该可以工作。

好的,因此将PHP代码视为

 $statusList = ArrayHelper::map(\app\models\SomeModel::find()->all(), 'id', 'title');

$response = [
     'success' => true,
     'id' => $id,
     'fullname' => $user->fullname,
     'statuslist' => $statusList,
];
因此我认为,
$statusList
类似于关联数组

 $statusList = Array
 (
        [1] => Status1
        [2] => Status2
        [3] => Status3
 )

 $statusList2d = Array();

 foreach ($statusList as $key => $value) {
    $innerArray = Array();
    array_push($innerArray, $key, $value);

    array_push($statusList2d, $innerArray);
 }

 $response = [
     'success' => true,
     'id' => $id,
     'fullname' => $user->fullname,
     'statuslist' => $statusList2d
 ];

现在您可以执行
json\u encode($response)
以获得预期的结果。

您只需在json\u encode之前使用array\u map即可

$arr['statuslist'] = array_map(null, array_keys($arr['statuslist']), $arr['statuslist']);

使用javascript(即客户端)的替代解决方案是使用
statuslist
对象上的方法(ECMAScript 2017)将其转换为数组

//假设您以JSON字符串的形式获取数据
让数据=“{”success:“true”,id:“20”,“fullname:“John Doe”,“statuslist:{”1:“Status1”,“2:“Status2”,“3:“Status3”}”;
让res=JSON.parse(数据)//解析字符串
//使用Object.entries将对象转换为具有[key,value]对的数组
res.statuslist=对象条目(res.statuslist);

控制台日志(res)//检查结果
因此,在php中修改您的
数组
结构。向我们展示您的php代码。javascript中有一些变通方法,您可以在获取json后重新构造json,但是更好的方法是在php中重新构造jsoncode@Joseph_J你必须从1开始,因为这是地位的价值,这是可行的,但只需更改变量$2d的名称,因为它不是有效的名称。