使用php为json输出创建多级数组

时间:2013-04-21 16:48:22

标签: php json multidimensional-array

到目前为止这是我的php。我首先添加了我的主要信息,然后添加了与该日期投票的用户的日期。

$id = $CURUSER["id"];

$eventid = $_GET['eventid'];

$z = SQL_Query_exec("SELECT * FROM cal_events WHERE eventid = '$eventid'");
$rowz = mysql_fetch_array($z);
$y = SQL_Query_exec("SELECT userid FROM cal_votes WHERE eventid = '$eventid'"); 
$y1 = mysql_num_rows($y);
$x = SQL_Query_exec("SELECT userid FROM cal_votes WHERE eventid = '$eventid' AND voted = 'no'");    
$x1 = mysql_num_rows($x);

$data = array();
            $data['eventid'] = $eventid;
            $data['eventname'] = $rowz['eventname'];
            $data['aboutevent'] = $rowz['aboutevent'];
            $data['lefttovote'] =   $x1;
            $data['enddate'] = date("D jS F Y",strtotime($rowz[enddate]));

 $caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
               while($rowcaldates = mysql_fetch_array($caldates)){
                $data['dates'][] =  date("D jS F Y",strtotime($rowcaldates[eventdates])); 


                    $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
                    $c1 = mysql_num_rows($b);
                     while($rowb = mysql_fetch_array($b)){
                        $data['dates']['names'][] = "$rowb[forename] $rowb[surname],";
                            }
            }

echo json_encode($data);

问题是我的json正在回归

    {"eventid":"23","eventname":"Mums Birthday","aboutevent":"Curry Night Alton 7pm","lefttovote":0,"enddate":"Wed 19th June 2013",
"dates":{"0":"Sat 23rd March 2013","
names":["John ,","Clare ,","Scott ,","Clare ,","Scott ,"],"1":"Sat 30th March 2013"}}

我试图输出这个。这只是一个悬而未决的例子,但我相信你会得到这个想法

 {"eventid":"23","eventname":"Mums Birthday","aboutevent":"Curry Night Alton 7pm","lefttovote":0,"enddate":"Wed 19th June 2013",
"dates":{"0":"Sat 23rd March 2013","
    names":["John,","Clare ,","Scott ,"}
"dates":{"1":"Sat 30th March 2013","
    names":["Clare ,","Scott ,"]}}

这样我就可以遍历日期并使用jquery mobile回显它们。我可以用直接的php做到这一点,因为我不需要将它们放入数组但是这个数组业务令人费解

更新 *

$data = array();
            $data['eventid'] = $eventid;
            $data['eventname'] = $rowz['eventname'];
            $data['aboutevent'] = $rowz['aboutevent'];
            $data['lefttovote'] =   $x1;
            $data['enddate'] = date("D jS F Y",strtotime($rowz[enddate]));

 $caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
               while($rowcaldates = mysql_fetch_array($caldates)){
                $date_data = array();
                $date_data[0] = date("D jS F Y",strtotime($rowcaldates[eventdates])); 


                    $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
                    $c1 = mysql_num_rows($b);
                     while($rowb = mysql_fetch_array($b)){
                        $date_data['names'] = "$rowb[forename] $rowb[surname],";
                        array_push($data,$date_data);
                            }

            }

echo json_encode($data);

输出

{"eventid":"23","eventname":"Mums Birthday","aboutevent":"Curry Night Alton 7pm","lefttovote":0,"enddate":"Wed 19th June 2013","0":{"0":"Sat 23rd March 2013","names":"John ,"},"1":{"0":"Sat 23rd March 2013","names":"Clare ,"},"2":{"0":"Sat 23rd March 2013","names":"Scott ,"},"3":{"0":"Sat 30th March 2013","names":"Clare ,"},"4":{"0":"Sat 30th March 2013","names":"Scott ,"}}

更新工作回答 *

 $caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
               while($rowcaldates = mysql_fetch_array($caldates)){
                $date_data = array();
                $date_data[0] = date("D jS F Y",strtotime($rowcaldates[eventdates])); 


                    $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
                    $c1 = mysql_num_rows($b);
                     while($rowb = mysql_fetch_array($b)){

                        $date_data['names'][] = "$rowb[forename] $rowb[surname],"; 

                            }
                        array_push($data,$date_data);   
            }

echo json_encode($data);

2 个答案:

答案 0 :(得分:4)

这不会起作用,因为您不能对多个childeNode使用相同的名称(即dates):

 { "eventid":"23",
   "eventname":"Mums Birthday",
   "aboutevent":"Curry Night Alton 7pm",
   "lefttovote":0,"enddate":"Wed 19th June 2013",
   "dates":{
           "0":"Sat 23rd March 2013",
           "names":["John Hunter,","Clare Kinnear,","Scott Kinnear,"
           },
   "dates":{
           "1":"Sat 30th March 2013",
           "names":["Clare Kinnear,","Scott Kinnear,"]
           }
  }

你应该在这样的数组中组合日期:

 { "eventid":"23",
   "eventname":"Mums Birthday",
   "aboutevent":"Curry Night Alton 7pm",
   "lefttovote":0,"enddate":"Wed 19th June 2013",
   "dates":[{
           "date":"Sat 23rd March 2013",
           "names":["John Hunter,","Clare Kinnear,","Scott Kinnear,"]
           },
           {
           "date":"Sat 30th March 2013",
           "names":["Clare Kinnear,","Scott Kinnear,"]
           }]
  }

要做到这一点,你可以这样做:

$n = 0;
$caldates = SQL_Query_exec("SELECT dateid,eventdates FROM cal_dates WHERE eventid = $eventid ORDER BY dateid ASC");
    while($rowcaldates = mysql_fetch_array($caldates)){
        $data->dates[$n]->date =  date("D jS F Y",strtotime($rowcaldates[eventdates])); 
        $b = SQL_Query_exec("SELECT forename,surname FROM cal_voted left join users on users.id = cal_voted.userid WHERE dateid = $rowcaldates[dateid] ");
        $c1 = mysql_num_rows($b);
        while($rowb = mysql_fetch_array($b)){
            $data->dates[$n]->names[] = "$rowb[forename] $rowb[surname],";
        }
        $n++;
    }

echo json_encode($data);

答案 1 :(得分:1)

在while循环中,创建一个名为$ date_data的数组。将日期存储在$ date_data [0]中,将名称存储在$ date_data ['names']中。如果是while,请将$ date_data按$ data ['dates] [] = $ date_date;

推入日期信息

另外,您不应将$ _GET变量直接放入查询中。确保使用预处理语句,或以某种方式转义值; - )