php - 如何将同一帖子的数据组合在一起?
问题描述
我从 2 个网站获取了一些帖子数据,一个网站有标题、日期、描述和链接,而另一个网站有标题和图片。
因此,如果两个网站的标题相同,我想将图像添加到其他帖子数据中。
这是我尝试过的:
$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];
}
}
如何将其他日期(链接、日期和描述)添加到带有标题和图像的数组中?
解决方案
改成:
if (in_array($title, $articles)){
$articles[$title]['img'] = $img;
} else {
//
}
推荐阅读
- python - 为什么我的乒乓球在球击到底部后卡住并且不显示“游戏结束”屏幕?
- android - 使用回收器视图从 Firebase 检索数据时出错:E/RecyclerView:未连接适配器;跳过布局
- python - Multiple commands with ssh using Python subprocess
- javascript - JavaScript 中的斐波那契数列
- sublimetext3 - Stylus 语法中的 Sublime Auto Quotes 行为非标准
- python-3.x - 使用 bs4 进行 Python 解析
- java - Lombok @Builder 与 Java 8 Lambda Builder 模式
- javascript - 如何做到这一点:每次按下空格键后,创建一个新的 span 元素并将插入符号放在那里?
- sql - 更新行组以从行的总和中删除特定的总量 SQL
- c# - XML 批量更改类型十六进制