问题
I'm getting some posts data from 2 websites, One website has the title, the date, the description and the link, While the other has the title and the image.
So I want to add the image to the other posts data if the titles from both websites are identical.
Here is what I tried:
$articles = [];
//Getting Data From 1st Website
$rss = simplexml_load_file($website1);
foreach ($rss->channel->item as $item) {
$post = [];
$post['title'] = (string)$item->title;
$post['link'] = (string)$item->link;
$post['date'] = (string)$item->pubDate;
$post['description'] = (string)$item->description;
$articles[$post['title']] = $post;
}
//Getting Data From The Second Website
require_once('simple_html_dom.php');
$html = file_get_html($website2);
foreach($html->find($articlesClass) as $article) {
$title = trim($article->find($titlesClasse, 0)->plaintext);
$img = $article->find($imagesClass[$i], 0)->{'src'};
if ( isset ($articles[$title])){
$articles[$title]['img'] = $img;
}
else {
$articles[$title] = ['title' => $title, 'img' => $img];
}
}
How to add the other date (link, date and description) to the array with the title and image?
回答1:
Change to:
if (in_array($title, $articles)){
$articles[$title]['img'] = $img;
} else {
//
}
来源:https://stackoverflow.com/questions/51134437/how-to-combine-the-data-for-the-same-post-together