我试图合并从2个不同的表格生成的数组 - '文章'和' 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;
}
'文章的输出'是:
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;
}
我需要插入'文章'阵列成' 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
)
)
换句话说,文章应该在类别数组中,如果文章'类别值与类别标题匹配。
我尝试在foreach循环中应用array_push' article_categories'但是我只为每个类别条目获取一个单独的数组,而不是整个数组。我不知道如何处理这个问题。
由于
答案 0 :(得分:0)
自己找到解决方案。不得不改变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;
}