如何将同一职位的数据合并在一起?

时间:2018-07-02 10:49:20

标签: php arrays rss feed simple-html-dom

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

如何将另一个日期(链接,日期和描述)添加到带有标题和图像的数组中?

1 个答案:

答案 0 :(得分:0)

更改为:

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