首页 > 解决方案 > 如何将同一帖子的数据组合在一起?

问题描述

我从 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];
        }   
    }   

如何将其他日期(链接、日期和描述)添加到带有标题和图像的数组中?

标签: phparraysrssfeedsimple-html-dom

解决方案


改成:

if (in_array($title, $articles)){
    $articles[$title]['img'] = $img;
} else {
     //
}

推荐阅读