简体   繁体   中英

Merging two RSS Feeds in PHP

I'm pulling in RSS feeds in PHP using the following code.

$var = (array) simplexml_load_file($rssfeed);

Everything works great. I'm able to loop through the RSS feed and do all the processing on the RSS feed within $var that I want to do. The problem is that I want to be able to merge two RSS feeds together.

So I use the same code to get the value from simplexml_load_file and pull out the items using $items = $var->item; but I can't figure out how to merge the two values from the item subarray between the two RSS feeds. I've tried using array_merge, array_combine and just stringing them together with a plus sign. I end up with either the values from the first RSS feed or the second, but not a merged set of values.

Does anyone have any ideas (speak slowly I'm a DBA by trade).

TIA, Denny

Try something like this, use a recursive function that converts the entire xml object to an array, array merge will not suffice after the merge you can convert it back to and object. I think the problem is if the two xml files being commbined has the same elements, they may be overwritten with the merge.

function recursive_object_to_array($obj) {
if(is_object($obj)) $obj = (array) $obj;
if(is_array($obj)) {
    $new = array();
    foreach($obj as $key => $val) {
        $new[$key] = recursive_object_to_array($val);
    }
}
else $new = $obj;
return $new;
}

if (file_exists('test_folder/rss1.xml') && file_exists('test_folder/rss2.xml')) {

  $rss1 =  recursive_object_to_array(simplexml_load_file('test_folder/rss1.xml'));
  $rss2 =  recursive_object_to_array(simplexml_load_file('test_folder/rss2.xml'));
  $rss_combined = (object) array_merge((array) $rss1, (array) $rss2);

  var_dump($rss1);    //content of first rss file
  var_dump($rss2);    //content of second rss file
  var_dump($rss_combined); // contents when recombined as object

 //this is my best bet, since the array keys are the same for the example  i   used, you need to  create an associative array and loop over it. 

  $all_rss[1] = $rss1;
  $all_rss[2] = $rss2;

  var_dump($all_rss); // creates asscociative array on which to loop

} else {
  exit('Failed to open xml files.');
}

So in the end I would use an array to access the elements. I used the xml file at the follwing link RSS W3schools

    // get the xml
   $rss1 =  recursive_object_to_array(simplexml_load_file('test_folder/rss1.xml'));
   $rss2 =  recursive_object_to_array(simplexml_load_file('test_folder/rss2.xml'));

  // create assoaciative array
  $all_rss[1] = $rss1;
  $all_rss[2] = $rss2;
   // loop over array
  foreach($all_rss as $key=>$value){

      echo $value['channel']['title'];
      echo '</br></br>';
      echo $value['channel']['link'];
      echo '</br></br>';
   }

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