我有三个mysql表:clients
,cars
和insurance
。每个客户可以拥有多辆汽车,每辆汽车都有多种保险政策。这是我的查询和sqlfiddle的链接:
SELECT
a.*,
b.id AS car_id, b.license_number, b.brand, b.model,
c.id AS insurance_id, c.insurance_company, c.start_date, c.end_date, c.price
FROM clients a
LEFT JOIN cars b ON a.id = b.client_id
LEFT JOIN insurance c ON b.id = c.car_id
http://sqlfiddle.com/#!2/773a5/1
因此,对于两个客户,其中第一个客户有两辆车和三个保险单,第二个客户没有车,没有保险,查询将返回4行。
我无法弄清楚如何循环此查询以返回以下结构,同时避免重复的客户端条目。
Array
(
[0] => Array
(
[id] => 1
[first_name] => John
[last_name] => Smith
[cars] => Array
(
[id] => 1
[license_number] => 'plate1'
[brand] => 'BMW'
[model] => 'E35'
[insurance] => Array
(
[id] => 1
[start_date] => '2015-02-10'
[end_date] => '2016-02-10'
[price] => '100'
)
(
[id] => 2
[start_date] => '2014-02-10'
[end_date] => '2015-02-10'
[price] => '50'
)
)
(
[id] => 2
[license_number] => 'plate2'
[brand] => 'VW'
[model] => 'Golf'
[insurance] => Array
(
[id] => 3
[start_date] => '2015-02-10'
[end_date] => '2016-02-10'
[price] => '100'
)
)
)
[1] => Array
(
[id] => 1
[first_name] => John
[last_name] => Smith
[cars] => NULL
)
)
答案 0 :(得分:4)
这很容易。它是关于使用assoc数组。您可以使用id,car_id和insurance_id作为数组键。
因此假设您将结果作为存储在$ rows变量中的获取的assoc行。简单的循环将是:
$result = array();
foreach($rows as $row) {
$id = $row['ID'];
$carId = $row['CAR_ID'];
$insId = $row['INSURANCE_ID'];
$result[$id]['first_name'] = $row['FIRST_NAME'];
$result[$id]['last_name'] = $row['LAST_NAME'];
if(!isset($result[$id]['cars'])) {
$result[$id]['cars'] = array();
}
if($carId && !isset($result[$id]['cars'][$carId])) {
$car = array(
'id' => $row['CAR_ID'],
'license_number' => $row['LICENSE_NUMBER'],
'brand' => $row['BRAND'],
'model' => $row['MODEL'],
'insurance' => array()
);
$result[$id]['cars'][$carId] = $car;
}
if($insId && isset($result[$id]['cars'][$carId]) && !isset($result[$id]['cars'][$carId]['insurance'][$insId])) {
$insurance = array(
'id' => $row['INSURANCE_ID'],
'start_date' => $row['START_DATE'],
'end_date' => $row['END_DATE'],
'price' => $row['PRICE']
);
$result[$id]['cars'][$carId]['insurance'][$insId] = $insurance;
}
}
答案 1 :(得分:3)
我不打算为你写出所有必要的字段,但它确实只归结为一个获取循环和一些if()
逻辑来构建子数组:
$info = array()
while($row = fetch from db) {
if(!isset($info[$row['userID']]) {
$info[$row['userID']] = array(
'first_name' => $row['first_name'],
...
'cars' => array()
);
}
if (!isset($info[$row['userID']]['cars'][$row['carID']]) {
etc...
}
}
对于您输入的每个子数组,您只需要另一个if()
测试来初始化该子数组,然后从您提取的$row
中填充它。
当你迭代行时,你会自然地建立你的结构。