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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch