gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:07:03 24 4
gpt4 key购买 nike

到目前为止,这是我的 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);

最佳答案

这行不通,因为您不能对多个子节点使用相同的名称(即 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);

关于php - 使用 php 为 json 输出创建多级数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16133635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com