如何将同一职位的数据合并在一起? [英] How to combine the data for the same post together?

查看:66
本文介绍了如何将同一职位的数据合并在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从2个网站上获得一些帖子数据,一个网站具有标题,日期,描述和链接,而另一个网站具有标题和图像.

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.

这是我尝试过的:

$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?

推荐答案

更改为:

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

这篇关于如何将同一职位的数据合并在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆