Php 从联接选择生成数组
我有这样一个选择加入原则:Php 从联接选择生成数组,php,json,foreach,left-join,Php,Json,Foreach,Left Join,我有这样一个选择加入原则: ->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", "proj
->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
)
)
谢谢。为了获得数据结构,请用打印件显示“选择”返回的内容,好吗 试试这个:
$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']
);
}
}
对于当前查询,您得到了什么输出?你能把它添加到你的问题中,这样我们就知道差异在哪里了吗?