从联接选择构建数组


Build array from join select

我有这个连接选择与教义:

->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 
        )
)

谢谢。

您能否显示您的"选择"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']
        );
    }
}