Php 将三个表的关系结果放入一个嵌套数组

Php 将三个表的关系结果放入一个嵌套数组,php,Php,我在谷歌上搜索我的问题的解决方案,但修女帮了我。 这里有三个表项,提要和图像。每个项目都有一个提要和一个或多个图像。 我有3个功能。一种是从items表返回记录,第二种是接收feeds_id(items表中的外键),然后从feeds表返回记录。第三个函数是返回所有与items\u id相关的图像 这些职能是: *要获取数据库中的所有项目: function get_items(){ return $query = Database::getInstance('db') -


我在谷歌上搜索我的问题的解决方案,但修女帮了我。 这里有三个表
提要
图像
。每个项目都有一个提要和一个或多个图像。 我有3个功能。一种是从items表返回记录,第二种是接收feeds_id(items表中的外键),然后从feeds表返回记录。第三个函数是返回所有与items\u id相关的图像

这些职能是:
*要获取数据库中的所有项目:

 function get_items(){
  return  $query = Database::getInstance('db')
        ->table('items')
        ->columns(
            'id',
            'items.rowid',
            'items.feed_id as feed_id',
            'title' )
        ->findAll();
}

*要从提要表中获取提要数据:

function get_feeds($id){
   return  $query = Database::getInstance('db')
        ->table('feeds')
        ->eq('id',$id)
         ->findAll();
}

*要从图像表中获取图像数据:

function get_images($id){
   return  $query = Database::getInstance('db')
        ->table('images')
        ->columns('items_id','src as image_url',
            'title as image_title',
            'alt')
        ->eq('items_id',$id)
        ->findAll();
   }
然后我有以下代码来调用这些函数并以jsonformat显示结果:

  $response['items'] = array();
  $response['feeds'] = array();
  $response['images'] = array();   

  foreach ($items = get_items() as $item) {

      $response['items'][] = array(
            'id' => (int)$item['rowid'],
            'feed_id' => (int)$item['feed_id'],
            'title' => $item['title'],
       );

      foreach ($feeds = get_feeds((int)$item['feed_id']) as $feed) {

         $response['feeds'][] = array(
            'title' => $feed['title'],
            'logo_url' => $feed['logo_url'],
            'site_url' => $feed['site_url'],
          );
        }

       foreach ($images = get_images($item['id']) as $image) {

         $response['images'][] = array(
            'id' => $image['items_id'],
            'url' => $image['image_url'],
            'thumb' =>  $_SERVER['SERVER_NAME'] . /myServer/images/thumbs/'. 'thumb_'.basename($image['image_url']),
             'title' => $image['image_title'],
             'alt' => $image['alt']
                );
            }
        }

        echo json_encode($response, JSON_PRETTY_PRINT);
因此,我的期望是获得json输出,如:

"items": [
        {
            "id": ,
            "feed_id": 
            "title":
            "feeds": [
                      {
                       "title": ,
                       "logo_url": ,
                       "site_url": "
                      }
                    ]
             "images": [
                         {
                          "id": ,
                          "url": ",
                          "thumb": 
                          "title": "",
                          "alt": ""
                         },
                         {
                          ....
                          }
                        ] 
        }]
我的意思是,每个项目数组都应该包含来自get_提要和get_images函数的相关数据的嵌套数组。 相反,我得到的回应是:

//here i select two items from my db
    "items": [
                { //first_item
                    "id": ,
                    "feed_id": 
                    "title":
                 },
                 { //second_item
                    "id": ,
                    "feed_id": 
                    "title":
                 }
              ],
       "feeds": [
                   { // feed data for first item
                     "title": ,
                      "logo_url": ,
                      "site_url": "
                   },
                   { // feed data for second item
                     "title": ,
                      "logo_url": ,
                      "site_url": "
                   }
               ],
                     "images": [
                                 { // image data for first item
                                  "id": ,
                                  "url": ",
                                  "thumb": 
                                  "title": "",
                                  "alt": ""
                                 },
                                 { // other images data 
                                  ....
                                  }
                                ] 
                }]
正如您所看到的,我得到的输出没有保持项目、提要和图像之间的关系,它们都是独立显示的

我的查询很好,但我怀疑我的
foreach
语句有错误

我可以通过在一个查询中连接这些树表来解决这个问题,但我不想这样做,因为我需要对每个表的输出进行验证和其他操作。
感谢您的帮助

我找到了解决方案。这很容易:)

就像:

 $response['items'][] = array(
            'id' => (int)$item['rowid'],
            'feed_id' => (int)$item['feed_id'],
            'title' => $item['title'],
            'feeds' => array(
             )
          'images' => array(
          )
   );