繁体   English   中英

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

[英]How to combine the data for the same post together?

我从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 {
     //
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM