SimplePie::merge_items PHP Method

merge_items() public static method

If you're merging multiple feeds together, they need to all have dates for the items or else SimplePie will refuse to sort them.
public static merge_items ( array $urls, integer $start, integer $end, integer $limit ) : array
$urls array List of SimplePie feed objects to merge
$start integer Starting item
$end integer Number of items to return
$limit integer Maximum number of items per feed
return array
    public static function merge_items($urls, $start = 0, $end = 0, $limit = 0)
    {
        if (is_array($urls) && sizeof($urls) > 0) {
            $items = array();
            foreach ($urls as $arg) {
                if ($arg instanceof SimplePie) {
                    $items = array_merge($items, $arg->get_items(0, $limit));
                } else {
                    trigger_error('Arguments must be SimplePie objects', E_USER_WARNING);
                }
            }
            usort($items, array(get_class($urls[0]), 'sort_items'));
            if ($end === 0) {
                return array_slice($items, $start);
            } else {
                return array_slice($items, $start, $end);
            }
        } else {
            trigger_error('Cannot merge zero SimplePie objects', E_USER_WARNING);
            return array();
        }
    }

Usage Example

Example #1
0
 /**
  * Get all items from the feed
  *
  * This is better suited for {@link http://php.net/for for()} loops, whereas
  * {@see get_items()} is better suited for
  * {@link http://php.net/foreach foreach()} loops.
  *
  * @see get_item_quantity
  * @since Beta 2
  * @param int $start Index to start at
  * @param int $end Number of items to return. 0 for all items after `$start`
  * @return array|null List of {@see SimplePie_Item} objects
  */
 public function get_items($start = 0, $end = 0)
 {
     if (!isset($this->data['items'])) {
         if (!empty($this->multifeed_objects)) {
             $this->data['items'] = SimplePie::merge_items($this->multifeed_objects, $start, $end, $this->item_limit);
         } else {
             $this->data['items'] = array();
             if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'entry')) {
                 $keys = array_keys($items);
                 foreach ($keys as $key) {
                     $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
                 }
             }
             if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_03, 'entry')) {
                 $keys = array_keys($items);
                 foreach ($keys as $key) {
                     $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
                 }
             }
             if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_10, 'item')) {
                 $keys = array_keys($items);
                 foreach ($keys as $key) {
                     $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
                 }
             }
             if ($items = $this->get_feed_tags(SIMPLEPIE_NAMESPACE_RSS_090, 'item')) {
                 $keys = array_keys($items);
                 foreach ($keys as $key) {
                     $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
                 }
             }
             if ($items = $this->get_channel_tags(SIMPLEPIE_NAMESPACE_RSS_20, 'item')) {
                 $keys = array_keys($items);
                 foreach ($keys as $key) {
                     $this->data['items'][] = $this->registry->create('Item', array($this, $items[$key]));
                 }
             }
         }
     }
     if (!empty($this->data['items'])) {
         // If we want to order it by date, check if all items have a date, and then sort it
         if ($this->order_by_date && empty($this->multifeed_objects)) {
             if (!isset($this->data['ordered_items'])) {
                 $do_sort = true;
                 foreach ($this->data['items'] as $item) {
                     if (!$item->get_date('U')) {
                         $do_sort = false;
                         break;
                     }
                 }
                 $item = null;
                 $this->data['ordered_items'] = $this->data['items'];
                 if ($do_sort) {
                     usort($this->data['ordered_items'], array(get_class($this), 'sort_items'));
                 }
             }
             $items = $this->data['ordered_items'];
         } else {
             $items = $this->data['items'];
         }
         // Slice the data as desired
         if ($end === 0) {
             return array_slice($items, $start);
         } else {
             return array_slice($items, $start, $end);
         }
     } else {
         return array();
     }
 }
All Usage Examples Of SimplePie::merge_items