从服务器返回多个json编码数组

时间:2022-10-24 14:01:31

I want to know how can I return multiple encoded JSON array from the server

我想知道如何从服务器返回多个编码的JSON数组

ex.

//client

$.ajax({
    url: 'items-details.php',
    type: 'POST',
    data: {member_id: 1},
    dataType: 'html',
    success: function(responseText) {

    }
});

//server, items-details.php

//some code here

then, the final output is ex. itemsData array and itemsCategories array, then I use json_encode() on both array. but how can I return both arrays to the client? I only know how to handle echo() - which is treated by the client as string

然后,最后的输出是ex。 itemsData数组和itemsCategories数组,然后我在两个数组上使用json_encode()。但是如何将两个数组都返回给客户端呢?我只知道如何处理echo() - 客户端将其视为字符串

before, I only use

以前,我只用

echo(json_encode(itemsData));

then the client will parse it .. but how can I return multiple json encoded array: itemsData and itemsCategories

那么客户端将解析它..但是如何返回多个json编码的数组:itemsData和itemsCategories

3 个解决方案

#1


0  

For example, you could create let's say a $response array, which could contain both the $itemsData and $itemsCategories arrays.

例如,您可以创建一个$ response数组,它可以包含$ itemsData和$ itemsCategories数组。

// $itemsData and $itemsCategories defined here

$response = array(
    $itemsData, $itemsCategories
);

return json_encode($response);

#2


0  

You can create a json object that holds both or your arrays and json_encode that object and render that: json_encode(items = { data: itemsData, categories: itemsCategories })

你可以创建一个json对象,它包含两个或你的数组和json_encode对象并呈现它:json_encode(items = {data:itemsData,categories:itemsCategories})

#3


0  

@edgeofmystery, you get right answer but if you return assoc
array it will be easier / more comfortable to parse it

@edgeofmystery,你得到正确答案,但如果你返回assoc数组,解析它会更容易/更舒服

$response = array(
    "itemsData"=>$itemsData, "itemsCategories"=>$itemsCategories
);

echo json_encode($response);

#1


0  

For example, you could create let's say a $response array, which could contain both the $itemsData and $itemsCategories arrays.

例如,您可以创建一个$ response数组,它可以包含$ itemsData和$ itemsCategories数组。

// $itemsData and $itemsCategories defined here

$response = array(
    $itemsData, $itemsCategories
);

return json_encode($response);

#2


0  

You can create a json object that holds both or your arrays and json_encode that object and render that: json_encode(items = { data: itemsData, categories: itemsCategories })

你可以创建一个json对象,它包含两个或你的数组和json_encode对象并呈现它:json_encode(items = {data:itemsData,categories:itemsCategories})

#3


0  

@edgeofmystery, you get right answer but if you return assoc
array it will be easier / more comfortable to parse it

@edgeofmystery,你得到正确答案,但如果你返回assoc数组,解析它会更容易/更舒服

$response = array(
    "itemsData"=>$itemsData, "itemsCategories"=>$itemsCategories
);

echo json_encode($response);