从mysql查询生成json数组

时间:2014-06-26 22:11:19

标签: php json

我正在尝试以下代码:

$order = array(); $imageURL = array(); $name = array();
        while($row = mysql_fetch_array($mysql->result)) {
            $order[] = $row["order"]; 
            $imageURL[] = $row["imageURL"];
            $name[] = $row["name"];
        }
        $res = array($order, $imageURL,$name);
        return json_encode($res);

但它没有以json格式输出任何想法?

输出:

[["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"],["previews\/en-1-1.gif","previews\/en-1-2.gif","previews\/en-1-3.gif","previews\/en-1-4.gif","previews\/en-1-5.gif","previews\/en-1-6.gif","previews\/en-1-7.gif","previews\/en-1-8.gif","previews\/en-1-9.gif","previews\/en-1-10.gif","previews\/en-1-11.gif","previews\/en-1-12.gif","previews\/en-1-13.gif","previews\/en-1-14.gif","previews\/en-1-15.gif","previews\/en-1-16.gif","previews\/en-1-17.gif","previews\/en-1-18.gif","previews\/en-1-19.gif","previews\/en-1-20.gif","previews\/en-1-21.gif","previews\/en-1-22.gif","previews\/en-1-23.gif","previews\/en-1-24.gif","previews\/en-1-25.gif","previews\/en-1-26.gif","previews\/en-1-27.gif","previews\/en-1-28.gif","previews\/en-1-29.gif","previews\/en-1-30.gif"],["Helasd you?","Where sasaddsdam?","Weasdd!","Tasasdther","AtsaddsaBeach","Cheasd Hotel","At the Hotel","aaaaaaaaat?","At the Market","Aasdt's","Mesadasd th","Shdsdsssg","sssss","On aaaa","Do you work or study?","aaaaaaa","At tadstation","aaaaaae Gym","How doasdto\u2026?","Planning a Trip","At adsk","At the asdurant","At the Inads00e9","My Taaaog","A Meetiaaaaay adher","Tourist sdsadn Centre","Saaaaing","Aaaaaa Match","Lookaaasd","At tasda"]]

3 个答案:

答案 0 :(得分:0)

我认为您只是在输出中使用了更好的格式,因为您目前使用三个数组进行设置,并且您必须引用每个记录。通过数字键在这些数组中的属性。

I've reconstructed an example原始数据库输出的样子:

$array = json_decode($json, true);

$row = array();
list($orders, $imageURLS, $names) = $array;
foreach($orders as $key => $val) {
  $row[] = array(
    'order' => $val,
    'imageURL' => $imageURLS[$key],
    'name' => $names[$key]
  );
}

所以在你的代码中,你应该试试这个:

$output = array();
while($row = mysql_fetch_array($mysql->result)) {
    $output[] = $row;
}
return json_encode($output);

And you'll get a much easier data structure to work with.示例:

[
  {
    "order": "1",
    "imageURL": "previews\/en-1-1.gif",
    "name": "Helasd you?"
  },
  {
    "order": "2",
    "imageURL": "previews\/en-1-2.gif",
    "name": "Where sasaddsdam?"
  }
]

答案 1 :(得分:0)

根据对原始问题的评论判断,看起来您正在寻找:

 $main_array = array() ;    


    while($row = mysql_fetch_array($mysql->result)) {

        $res = array($row["order"], $row["imageURL"],$row["name"]);   
        $main_array[] = $res; 
    }

    return json_encode($main_array);

答案 2 :(得分:0)

输出正确。一个json数组,包含三个json数组。但我想你希望每个元素都是一个单独的数组/对象,包含orderimageURLname。如果这是正确的,请尝试类似:

$result = array();

while($row = mysql_fetch_array($mysql->result)) {
$result[] = array("order"=>$row["order"], "imageURL" => $row["imageURL"],"name"=> $row["name"])
}
$res = array($order, $imageURL, $name);
return json_encode($result);