我有这个连接选择与学说:
->select("concat(c.name, ' ', c.second_name) as name_client","c.id as id_client", "p.id as id_project", "s.id as id_status","p.renew_date","p.domain")
->from("clients", "c")
->leftJoin("c", "projects", "p", "c.id = p.id_client")
->leftJoin("c", "todos", "t", "t.id = c.id")
->orderBy('p.renew_date', 'ASC');
我需要像这样的json输出:
[
{
"id":1,
"name_client" : "Tim",
"projects" : [
{
"id" : 1
"domain" : "www.test.com"
},
{
"id" : 1
"domain" : "www.test.com"
}
],
"todos": [
{
"id" : 1
"text" : "do something"
},
{
"id" : 1
"text" : "do something"
}
]
}
]
如何循环我的数据以获得此数组结构? 这是我的数组数据:
Array
(
[0] => Array
(
[name_client] => Aaaaaaa
[id_todo] =>
[text] =>
[id_client] => 9
[id_project] => 13
[renew_date] =>
[domain] => xxx.it
)
[1] => Array
(
[name_client] => Bbbbbb
[id_todo] =>
[text] =>
[id_client] => 8
[id_project] => 12
[renew_date] => 2016-01-23
[domain] => vvvv.it
)
[2] => Array
(
[name_client] => Dddddddd
[id_todo] =>
[text] =>
[id_client] => 1
[id_project] => 1
[renew_date] => 2016-03-19
[domain] => eeeeee.it
)
[3] => Array
(
[name_client] => Assssass
[id_todo] =>
[text] =>
[id_client] => 5
[id_project] => 8
[renew_date] => 2016-03-26
[domain] => erreerr.net
)
[4] => Array
(
[name_client] => Eeeeeeee
[id_todo] =>
[text] =>
[id_client] => 3
[id_project] => 5
[renew_date] => 2016-04-27
[domain] => rrrrrrr.it
)
[5] => Array
(
[name_client] => Edsdee
[id_todo] =>
[text] =>
[id_client] => 7
[id_project] => 10
[renew_date] => 2016-07-19
[domain] => rrrrrrr.com
)
[6] => Array
(
[name_client] => Dddddddd
[id_todo] =>
[text] =>
[id_client] => 6
[id_project] => 9
[renew_date] => 2016-07-27
[domain] => rrrrrrrrr.it
)
[7] => Array
(
[name_client] => wwwwwwwww
[id_todo] =>
[text] =>
[id_client] => 4
[id_project] => 6
[renew_date] => 2016-09-24
[domain] => rrttttrrr.it
)
[8] => Array
(
[name_client] => yyyyyyyy
[id_todo] => 1
[text] => Todo!!!!!
[id_client] => 2
[id_project] => 4
[renew_date] => 2016-09-29
[domain] => uuuuuuuuu.it
)
)
感谢。
答案 0 :(得分:1)
你能否展示你的选择"选择"请返回whith print_r,以获得数据结构?
试试这个:
$clients = array();
foreach ($queryResults as $entry) {
if (! isset($clients[$entry['id_client']])) {
$clients[$entry['id_client']] = array(
'id' => $entry['id_client'],
'name_client' => $entry['name_client'],
'todos' => array(),
'projects' => array()
);
}
if (! empty($entry['id_todo'])) {
$clients[$entry['id_client']]["todos"][] = array(
'id' => $entry['id_todo'],
'text' => $entry['text']
);
}
if (! empty($entry['id_project'])) {
$clients[$entry['id_client']]["projects"][] = array(
'id' => $entry['id_project'],
'domain' => $entry['text']
);
}
}