简体   繁体   English

在PHP中合并两个RSS Feed

[英]Merging two RSS Feeds in PHP

I'm pulling in RSS feeds in PHP using the following code. 我使用以下代码在PHP中提取RSS feed。

$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. 我可以遍历RSS提要,并在$ var内对RSS提要进行所有处理。 The problem is that I want to be able to merge two RSS feeds together. 问题是我希望能够将两个RSS feed合并在一起。

So I use the same code to get the value from simplexml_load_file and pull out the items using $items = $var->item; 因此,我使用相同的代码从simplexml_load_file获取值,并使用$items = $var->item;提取$items = $var->item; but I can't figure out how to merge the two values from the item subarray between the two RSS feeds. 但我不知道如何合并两个RSS feed之间的item子数组中的两个值。 I've tried using array_merge, array_combine and just stringing them together with a plus sign. 我试过使用array_merge,array_combine并将它们与加号一起串起来。 I end up with either the values from the first RSS feed or the second, but not a merged set of values. 我最终得到的是第一个RSS提要或第二个RSS提要中的值,而不是合并的值集。

Does anyone have any ideas (speak slowly I'm a DBA by trade). 是否有人有任何想法(慢慢地说,我是一名DBA)。

TIA, Denny TIA丹尼

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. 尝试类似这样的事情,使用递归函数将整个xml对象转换为数组,在合并之后,数组合并将不足以将其转换回and对象。 I think the problem is if the two xml files being commbined has the same elements, they may be overwritten with the merge. 我认为问题在于,如果要合并的两个xml文件具有相同的元素,则合并可能会覆盖它们。

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 我在以下链接RSS W3schools中使用了xml文件

    // 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>';
   }

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

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