I hope, I formulated my title right. I want to understood how to merge to 2 arrays into 1 array.
Simple example of what I want to do:
I have 2 database tables, like blog_posts and users.
I want to select data from blog_posts and foreach post I want to select information for "addedby" like username, user_image, etc.
With echo I managed to just show values like <?php echo $user['username']; ?>
and <?php echo $blog['title']; ?>
I want to return those values, like in mvc pattern. How I was reading from different topics, I have to store data into arrays, to return them, with one array there's no problem, but I don't understand how can I return them both.
public function getBlogPostList(){
try{
$sortby = "SELECT * FROM blog_posts";
$stmt = $this->connCommunity->prepare("$sortby");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $post){
$addedby = $post['addedby'];
$stmt2 = $this->connUsers->prepare("SELECT * FROM users WHERE id=('$addedby')");
$stmt2->execute();
$result2 = $stmt2->fetchAll();
$blog_post = array();
foreach($result as $post) {
$blog_post[] = $post;
}
foreach($result2 as $user) {
array_merge($blog_post, $user);
}
}
return $blog_post;
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
And displaying arrays like:
<?php
foreach ($BlogModel as $BlogModel)
{
echo '<tr><td><a href="index.php?id='.$BlogModel['id'].'">'.$BlogModel['id'].'</a></td><td>'.$BlogModel['title'].'</td><td>'.$BlogModel['username'].'</td></tr>';
}
?>