PHP数组格式错误

时间:2017-02-06 12:08:04

标签: php arrays json while-loop getjson

我有一个查询,我希望将结果插入到数组中,所以我将其编码为JSON,但我的问题是我希望数据设置如下:

  

array [0] = project1,project2,project3;   array [1] = item1,item2,item3;

我有这个:

  

array [0] = project1;   array [1] = project2;   array [2] = project3;

依旧......

这是我到目前为止所做的:

$info = array();

    $items = mysql_query("SELECT * FROM `vision`.`projects` WHERE  proj_area = 'area_1'");

        if (mysql_num_rows($items) != 0) { 
            while($proj = mysql_fetch_array($items)) {      

            $proj_name = $proj['proj_name'];
            $proj_beg = $proj['proj_beg'];
            $proj_end = $proj['proj_end'];

            array_push($info, $proj_name, $proj_beg, $proj_end );
        } 
    }

    echo json_encode($info);

我的查询结果给了我这些结果:

  

[" nome"," 0000-00-00"," 0000-00-00"," Projeto 2",& #34; 2016-12-12"," 2020-07-30"," Projeto"," 2017-02-03",&#34 ; 2018-03-10"]

这是我的$ .getJSON代码:

$.getJSON("includes/get_area.php",function(data){

                    console.log(data);
                    })

我做错了什么?

1 个答案:

答案 0 :(得分:3)

试试这个;这将在三个数组索引中的每一个中添加一个列表。

$info = array();

$items = mysql_query("SELECT * FROM `vision`.`projects` WHERE  proj_area = 'area_1'");
if (mysql_num_rows($items) != 0) {
    while($proj = mysql_fetch_array($items)) {
        $info[0][] = $proj['proj_name'];
        $info[1][] = $proj['proj_beg'];
        $info[2][] = $proj['proj_end'];
    }
}

echo json_encode($info);