【问题标题】:How to combine the data for the same post together?如何将同一帖子的数据组合在一起?
【发布时间】:2018-12-10 13:53:57
【问题描述】:

我从 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 ( isset ($articles[$title])){ $articles[$title]['img'] = $img; } else { $articles[$title] = ['title' => $title, 'img' => $img]; }
  • 我想将其他数据添加到该数组中,这些数据` $post['link'] = (string)$item->link; $post['date'] = (string)$item->pubDate; $post['description'] = (string)$item->description;`
  • @mickmackusa,这个问题已经解决了,如果你愿意我可以删除它,如果你能帮忙,还有另一个相关的问题
  • 我认为删除这个是一个简单的选择。如果您有一个细节敏感的问题,您可以发送电子邮件。否则,您可以发布新问题。
  • 您也可以删除其他页面上现在没有意义的任何cmets。始终尽量保持网站整洁。

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


【解决方案1】:

改为:

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

【讨论】:

  • 我想添加其他数据而不仅仅是图像
  • 问题中的代码添加了图像,我需要添加其他数据
  • @Ashley 哪些数据?!来自网站 1?
猜你喜欢
  • 2020-03-05
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 2011-07-22
  • 2021-09-26
  • 2021-08-17
相关资源
最近更新 更多