Sorting an array of SimpleXML objects

后端 未结 5 1218

I\'ve read what I\'ve found on Stackoverflow and am still unclear on this.

I have an array of SimpleXML objects something like this:

array(2) {
  [0]         


        
5条回答
  •  感动是毒
    2020-12-02 00:50

    it's a old thread but here is my solution that a use to extract information from a rss feed in order to sort by title

    $xml =  simplexml_load_file('rss.xml');
    $msg = array();
    $msg_count = $xml->channel->item->count();
    
    for ($x=0;$x<$msg_count;$x++){
        $msg[$x]['titl'] = (string)$xml->channel->item[$x]->title;
        $msg[$x]['link'] = (string)$xml->channel->item[$x]->link;
        $msg[$x]['date'] = (string)$xml->channel->item[$x]->pubDate;
        $msg[$x]['time'] = strtotime(substr((string)$xml->channel->item[$x]->pubDate,4));
        $msg[$x]['desc'] = (string)$xml->channel->item[$x]->description;
        $msg[$x]['auth'] = (string)$xml->channel->item[$x]->author;
    }
    
    foreach ($msg as $key => $row) {
        $titl[$key] = $row['titl'];
        $link[$key] = $row['link'];
        $pdat[$key] = $row['date'];
        $time[$key] = $row['time'];
        $cate[$key] = $row['cate'];
        $desc[$key] = $row['desc'];
        $auth[$key] = $row['auth'];
    }
    
    array_multisort(
    //change $titl to any variable created by "foreach"
        $titl, SORT_ASC,SORT_NATURAL | SORT_FLAG_CASE,
        $msg); 
    

提交回复
热议问题