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