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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?