简体   繁体   中英

How to combine the data for the same post together?

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?

Change to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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