Sql响应如下所示:名称|位置|日期|地址。行可以包含相同的名称,位置和日期,但每次都会更改地址。所以,我需要把这个响应放到这样的数组中:
while($r = mysqli_fetch_assoc($q_res)){
$i = 0;
if(!in_array($r['watcher_name'], $daily_quests)){
$daily_quests[$i]['name'] = $r['name'];
$daily_quests[$i]['date'] = $r['date'];
$daily_quests[$i]['locality'] = $r['locality'];
}
else{
$daily_quests['address'] = $intrnal_addr[$i => $r['address']];
}
}
我已尝试使用此代码,但确实无法工作=(
=INDEX(A:A;ROUND(ROW(A1)/2);1)
答案 0 :(得分:1)
试试这个:
//Initialisation
$name = '';
$daily_quests = array();
$i = -1;
while($r = mysqli_fetch_assoc($q_res)){
//If the row is different than precedent
if (strcmp($r['name'], $name) != 0)
{
$i++;
$name = $r['name'];
$daily_quests[$i] = array();
$daily_quests[$i]['name'] = $r['name'];
$daily_quests[$i]['date'] = $r['date'];
$daily_quests[$i]['locality'] = $r['locality'];
$daily_quests[$i]['address'] = array();
}
// For each row, we must add address
$daily_quests[$i]['address'][] = $r['address']
}
但是,我们假设名称是唯一的。与id进行比较更安全。
答案 1 :(得分:0)
您可以通过对MySQL查询进行分组来解决此问题:
select
Name, Date, Locality, group_concat(Address SEPARATOR '|') as addresses
from __table__
group by Name, Date, Locality;
然后在php:
while($r = mysqli_fetch_assoc($q_res)){
$r['Address'] = explode('|', $r['addresses']);
unset($r['addresses']);
$daily_quests[] = $r;
}