以下代码:
$services = $this->find('all', array(
'contain' => array('Category','Country'),
'joins' => array(
array('table' => 'services_to_categories',
'alias' => 'ServiceCategory',
'type' => 'INNER',
'conditions' => array(
'ServiceCategory.service_id = Service.id'
)
)
),
'group' => 'Service.id'
));
return $services;
给我输出结果:
Array
(
[0] => Array
(
[Service] => Array
(
[id] => 6
[job_title] => director
)
[Category] => Array
(
[0] => Array
(
[id] => 1
[category] => Accounting & Financial
)
[1] => Array
(
[id] => 3
[category] => Awards & Incentives
)
[2] => Array
(
[id] => 7
[category] => Data Management
)
)
)
[1] => Array
(
[Service] => Array
(
[id] => 11
[job_title] => d
)
[Category] => Array
(
[0] => Array
(
[id] => 7
[category] => Data Management
)
[1] => Array
(
[id] => 10
[category] => Internet Services
)
)
)
[2] => Array
(
[Service] => Array
(
[id] => 12
[job_title] => e
)
[Category] => Array
(
[0] => Array
(
[id] => 4
[category] => Business Consulting
)
[1] => Array
(
[id] => 7
[category] => Data Management
)
)
)
)
我需要输出如下所示:
Accounting & Financial
job_title: director
Awards & Incentives
job_title: director
Data Management
job_title: director
job_title: d
job_title: e
Internet Services
job_title: d
Business Consulting
job_title: e
如何在上面的输出中使用这个数组变量$ services?
答案 0 :(得分:1)
基本上,我所做的只是遍历数组并创建一个新类别,其中类别为键,作业标题为值数组。然后我只是通过我的新阵列循环并吐出关键和值。
<?php
$category_array = array();
// LOOP THROUGH THE MAIN ARRAY AND RESTRUCTURE IT
foreach ($start_array AS $array_element) {
foreach ($array_element['Category'] AS $category) {
$category_array[$category['category']][] = $array_element['Service']['job_title'];
}
}
// LOOP THROUGH OUR NEW ARRAY AND PRINT OUT THE KEY AND VALUES
foreach ($category_array AS $department => $job_title_array) {
print $department;
foreach ($job_title_array AS $job_title) {
print "\njob_title: ".$job_title;
}
print "\n\n";
}
答案 1 :(得分:0)
您无法使用查询解决此问题。
你想要的是这样的:
+------------------------+----------+
| Accounting & Financial | director |
+------------------------+----------+
| Data Management | director |
| +----------+
| | d |
| +----------+
| | e |
+------------------------+----------+
显然,SQL适用于表,而不是嵌套树,因此无法完成。
1)SQL会给你(使用GROUP BY Service.job_title
):
+------------------------+----------+
| Accounting & Financial | director |
+------------------------+----------+
| Data Management | d |
+------------------------+----------+
| Data Management | e |
+------------------------+----------+
2)或者这个(使用GROUP BY Category.category
):
+------------------------+----------+
| Accounting & Financial | director |
+------------------------+----------+
| Data Management | director |
+------------------------+----------+
3)或者这个(不使用GROUP BY
):
+------------------------+----------+
| Accounting & Financial | director |
+------------------------+----------+
| Data Management | director |
+------------------------+----------+
| Data Management | d |
+------------------------+----------+
| Data Management | e |
+------------------------+----------+
如果您丢失了数据:Data Management | director
不再出现在结果中。
在案例2中,您也会丢失数据:Data Management | d
和Data Management | e
不再出现在结果中。
所以你必须使用案例3.然后手动(在代码中)将此结果转换为您想要的嵌套数组。
PS:这些是伪结果。实际结果取决于与数据库有关的各种因素。