dqhnp44220 2015-01-16 01:13
浏览 34
已采纳

使用公共值将两个表中的两个数组合并为一个数组

I'm trying to combine arrays generated from 2 different tables - 'articles' and 'article_categories'. The output of 'article_categories' is:

Array
(
[0] => Array
    (
        [id] => 0
        [title] => local
    )

[1] => Array
    (
        [id] => 1
        [title] => politics
    )

[2] => Array
    (
        [id] => 2
        [title] => economics
    )
)

The function code is:

public function newsGetCategoryList() {
        $result = $this->db->select('SELECT * FROM article_categories ORDER BY id');   
        return $result;
}

The output of 'articles' is:

Array
(
[0] => Array
    (
        [id] => 0
        [title] => Article 1
        [content] => content
        [category] => local
    )

[1] => Array
    (
        [id] => 1
        [title] => Article 2
        [content] => content
        [category] => local
    )

[2] => Array
    (
        [id] => 2
        [title] => Article 2
        [content] => content
        [category] => local
    )
)

The function code is:

public function newsGetCategoryArticles($category) {
       $result = $this->db->select('SELECT * FROM articles WHERE category_id = :category', array('category' => $category));            
       return $result;
    }

I need to insert 'articles' array into 'article_categories' so the output becomes:

Array
(
[0] => Array
    (
        [id] => 0
        [title] => local
        [articles] => Array
        (
        [0] => Array
            (
               [id] => 0
               [title] => Article 1
               [content] => content
               [category] => local
            )

        [1] => Array
            (
               [id] => 1
               [title] => Article 2
               [content] => content
               [category] => local
            )

        [2] => Array
            (
               [id] => 2
               [title] => Article 2
               [content] => content
               [category] => local
            )
        )
)
[1] => Array
    (
        [id] => 1
        [title] => politics
    )

[2] => Array
    (
        [id] => 2
        [title] => economics
    )
)

In other words, articles should be inside the category array if articles' category value matches to category's title.

I have tried applying array_push in foreach loop on 'article_categories' but I was only getting a separate array for each category entry, rather than the whole array. I have no ideas how to approach this.

Thanks

  • 写回答

1条回答 默认 最新

  • duancuo1234 2015-01-17 16:23
    关注

    Found the solution myself. Had to change newsGetCategoryList()

    public function newsGetCategoryList() {
            $array = $this->db->select('SELECT * FROM article_categories ORDER BY id');
            $result = array();
            foreach ($array as $value) {
                $articles = $this->newsGetCategoryArticles($value['title']);
                array_push($value, $articles);
                $result[] = $value;                
            } 
            return $result;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改