Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
在php中输出json数组_Php_Json - Fatal编程技术网

在php中输出json数组

在php中输出json数组,php,json,Php,Json,我现在有这个json: {"quest_id":"1","quest_title":"Buy 3 pints of draft and a large pizza and then get desert","quest_price":"15","quest_points":"100"}{"quest_id":"2","quest_title":"Hello WOrld","quest_price":"50","quest_points":"10"} 我想知道如何输出这个: {"quests":

我现在有这个json:

{"quest_id":"1","quest_title":"Buy 3 pints of draft and a large pizza and then get desert","quest_price":"15","quest_points":"100"}{"quest_id":"2","quest_title":"Hello WOrld","quest_price":"50","quest_points":"10"}
我想知道如何输出这个:

{"quests":  {"quest_id":"1","quest_title":"Buy 3 pints of draft and a large pizza and then get desert","quest_price":"15","quest_points":"100"}{"quest_id":"2","quest_title":"Hello WOrld","quest_price":"50","quest_points":"10"}
}
以下是php中的代码:

while($result=mysql_fetch_array($number, MYSQL_ASSOC)){
        print(json_encode($result));

    }
试试这个:

$result = array('quests' => array());
while($row = mysql_fetch_array($number, MYSQL_ASSOC)){
    $result['quests'][] = $row
}
echo json_encode($result);

如果我正确理解您要做的事情,请获取一个包含所有行的JSON数据包,然后循环将它们放入一个数组中,然后对整个数组进行编码:

<?php
  $result = mysql_query($query);
  $out = array('quests' => array());
  while ($row = mysql_fetch_assoc($result)) {
      $out['quests'][] = $row;
  }
  print json_encode($out);
?>


您尝试过什么吗?听起来很容易做到……在这里花点时间:你的第二位JSON实际上是无效的。你需要把这两个任务列在一个列表或类似的东西里。你想完成任务吗?我认为最好的学习方法是PHP手册,正如@TheNail在上面写的那样。将
$out[]=$row
更改为
$out['quests'][]=$row
又错了。需要精确:
$out['quests'][]=$row。如果只有
$out['quests]=$row,每次你的数组都会被改写。我相信有一种表达方式可以表达这样一个明显的错误被抓了两次,但我不能把它放在哪里。