How to combine the data for the same post together?

我与影子孤独终老i 提交于 2020-01-15 10:23:09

问题


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.

Here is what I tried:

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


回答1:


Change to:

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


来源:https://stackoverflow.com/questions/51134437/how-to-combine-the-data-for-the-same-post-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!