我想做的是基于MYSQL表中的一列(event_id)创建一个多维关联数组。
我想要的一个例子是这个数组,而php代码就是我到目前为止所拥有的
[
'event_id #1' : [{id=24, name: "Joe", code: "dsf34fsf"},{id=25,name: "Bob", code: "nlwr32"],
'event_id #2' : [{id=54, name: "Kevin", code: "sdfs"},{id=57,name: "Dan"}, code: "i5er343"]
]
//Get tickets for the event if the user bought them
$query = mysqli_query($db_conx, "SELECT id, name, event_id, code FROM tickets WHERE user_id='$user_id' ORDER BY event_id DESC");
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$ticket_id = $row["id"];
$code = $row["code"];
$name = $row["name"];
$event_id = $row["event_id"];
$tickets[] = array($event_id => array('id' => $ticket_id, 'n' => $name, 'c' => $code));
}
答案 0 :(得分:1)
您只需要更改:
$tickets[] = array($event_id => array('id' => $ticket_id, 'n' => $name, 'c' => $code));
收件人:
$tickets[$event_id][] = array('id' => $ticket_id, 'n' => $name, 'c' => $code);
对于您的示例数据,其结果将如下所示:
Array
(
[event_id #1] => Array
(
[0] => Array
(
[id] => 24
[n] => Joe
[c] => dsf34fsf
)
[1] => Array
(
[id] => 25
[n] => Bob
[c] => nlwr32
)
)
[event_id #2] => Array
(
[0] => Array
(
[id] => 54
[n] => Kevin
[c] => sdfs
)
[1] => Array
(
[id] => 57
[n] => Dan
[c] => i5er343
)
)
)