我有两个表用户(id、userid、fn、ln)和userdetails(id、userid、image、history、location、activity),我为第一个表编写了一个查询以检索所有数据,我只想从第二个表中检索历史记录和位置。
我已经检索到数组,并将其发送到json_encode。现在,我希望检索历史记录、位置并创建一个新的密钥历史记录,并希望将历史位置值添加到历史键中。
我需要这些查询和json格式。
[{ "id":"81","user_id":"2011","fn":"asd.","ln":"wer","History":{ "id":"350",“历史”:"make1",“位置”:"qwe“},{ "id":"82","user_id":”2012年“,"fn":"asd1","ln":"wer1",”历史“:{ "id":"350",“历史”:"make2",“位置”:"qwe2“}]
发布于 2014-11-15 14:13:37
Userdetails表包含每个用户的multiple records。所以你需要做子查询,得到结果,然后形成一个mulch-dimensional array。终于,encode as a JSON。
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
$result = $sth->fetchAll();
$i=0;
foreach($result as $data) {
$final_array[$i]['id'] = $data['id'];
$final_array[$i]['userid'] = $data['userid'];
$final_array[$i]['fn'] = $data['fn'];
$final_array[$i]['ln'] = $data['ln'];
$sth2 = $dbh->prepare("SELECT location,activity FROM userdetails WHERE userid= ".$data['id']."");
$sth2->execute();
$result2 = $sth2->fetchAll();
$j=0;
$history_array = array();
foreach($result2 as $data2) {
$history_array[$j] = array("location" => $data2['location'], "activity " => $data2['activity ']);
$j++;
}
$final_array[$i]['history'] = $history_array;
$i++;
}
echo json_encode($final_array);https://stackoverflow.com/questions/26946543
复制相似问题