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

Combining two arrays from two tables into a single array using a common value

我正在尝试合并从 2 个不同的表生成的数组 - 'articles' 和 'article_categories'。 'article_categories' 的输出是:

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

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

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

功能码为:

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

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

功能码为:

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

我需要将 'articles' 数组插入到 'article_categories' 中,因此输出变为:

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

换句话说,如果文章的类别值与类别的标题匹配,则文章应该在类别数组中。

我曾尝试在 'article_categories' 上的 foreach 循环中应用 array_push,但我只为每个类别条目获取了一个单独的数组,而不是整个数组。我不知道如何处理这个问题。

谢谢

自己找到了解决方案。必须更改 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;
    }