【问题标题】:Returning a multidimensional array as json response将多维数组作为 json 响应返回
【发布时间】:2013-07-25 04:46:50
【问题描述】:

我正在处理一个 jquery 分页,其中我有 Json 响应来返回要加载到特定页面和其他支持绑定元素值中的数据。为此,我需要在每次点击页面时返回一个二维数组作为 Json 响应。我已经声明了二维数组并像下面一样返回它,我认为这样做是错误的,需要纠正。

我的 php 页面中的代码:

     header("Content-Type: application/json");   
 dataset array from which pagination will select the page data:

    $data = array("a","b","c","d","e","f","g","h","i","j",);

    2 dimensional array that I wish to return as response:

$response_multi = array(
    $currentPage,
    $hasNextPage,
    $hasPreviousPage,
    $maxPage,
    $dataCount,
    $dataResponse = array($pageSize),
    $pageSize   
);
    ######### variables and array within $response_multi array will be set and the return section is like ############

    echo json_encode($response_multi );

它不工作。请任何人!提前致谢。 :)

【问题讨论】:

  • 声明 $pagesize 外部为数组并像这样传递它 $dataResponse = $pageSize,
  • 多维数组中所有其他变量的值是多少?这应该有效。

标签: php json multidimensional-array pagination


【解决方案1】:

这样试试

$pageSize = array(1,2,3,4)


$response_multi[] =  $currentPage;
$response_multi[] =  $hasNextPage;
$response_multi[] =  $hasPreviousPage;
$response_multi[] =  $maxPage;
$response_multi[] =  $dataCount;
$response_multi['dataResponse'] =  $pageSize;
$response_multi[] =  $pageSize;

echo json_encode($response_multi);

【讨论】:

  • 谢谢@sundar,除了我必须使用 $response_multi[]= $dataResponse 在该数组中设置一个数组之外,这很有效。:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 2021-01-24
  • 2019-07-27
  • 2023-03-14
相关资源
最近更新 更多