How to randomize SimpleXML object?

Discussion in 'PHP' started by pixads, Mar 18, 2010.

  1. #1
    I need to randomize a SimpleXML object and output it as a rss feed.


    How should this be done?

    Thanks
     
    pixads, Mar 18, 2010 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    convert it to an array and use shuffle? or count the items in the simplexml file and push each # into an array and shuffle that then use it to loop through the xml.

    like so (untested but basic idea should work) :
    
     <?php
    $xml = simplexml_load_file($yourfile);
    $xml_cnt = count($xml->items);
        $allitems = range(0,$xml_cnt);
        $all = shuffle($allitems);
    
    foreach($all as $curr) 
    {
    
    // output here.
    
    }
    
    ?> 
    Code (markup):
     
    shallowink, Mar 18, 2010 IP