tags:

views:

35

answers:

2

Crux of my problem: I've got an XML file that returns 20 results. Within these results are all the elements I need to get. Now, I need to return them in a random order, and be able to specifically work with item 1, items 2-5, and items 6-17.

Idea 1: Use this script to convert the object to an array, which I can shuffle through. This is close to working, but a few of the elements I need to get are under a different namespace, and I don't seem to be able to get them. Code:

/*
 * Convert a SimpleXML object into an array (last resort).
 *
 * @access public
 * @param object $xml
 * @param boolean $root - Should we append the root node into the array
 * @return array
 */

function xmlToArray($xml, $root = true) {
if (!$xml->children()) {
    return (string)$xml;
}

$array = array();
foreach ($xml->children() as $element => $node) {
    $totalElement = count($xml->{$element});

    if (!isset($array[$element])) {
        $array[$element] = "";
    }

    // Has attributes
    if ($attributes = $node->attributes()) {
        $data = array(
            'attributes' => array(),
            'value' => (count($node) > 0) ? xmlToArray($node, false) : (string)$node
            // 'value' => (string)$node (old code)
        );

        foreach ($attributes as $attr => $value) {
            $data['attributes'][$attr] = (string)$value;
        }

        if ($totalElement > 1) {
            $array[$element][] = $data;
        } else {
            $array[$element] = $data;
        }

    // Just a value
    } else {
        if ($totalElement > 1) {
            $array[$element][] = xmlToArray($node, false);
        } else {
            $array[$element] = xmlToArray($node, false);
        }
    }
}

if ($root) {
    return array($xml->getName() => $array);
} else {
    return $array;
}
}

$thumbfeed = simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?q=skadaddlemedia&max-results=20&orderby=published&prettyprint=true');

$xmlToArray = xmlToArray($thumbfeed);
$thumbArray = $xmlToArray["feed"];
for($n = 0; $n < 18; $n++){
$title = $thumbArray["entry"][$n]["title"]["value"];
$desc = $thumbArray["entry"][0]["content"]["value"];
$videoUrl = $differentNamespace;
$thumbUrl = $differentNamespace;
}

Idea 2: Continue using my working code that is getting the information using a foreach, but store each element in an array, then use shuffle on that. I'm not precisely sure how to write to an array within a foreach loop and not write over one another, though. Working code:

foreach($thumbfeed->entry as $entry){
    $thumbmedia = $entry->children('http://search.yahoo.com/mrss/')
        ->group
    ;
    $thumb = $thumbmedia->thumbnail[0]->attributes()->url;
    $thumburl = $thumbmedia->content[0]->attributes()->url;
    $thumburl1 = explode("http://www.youtube.com/v/", $thumburl[0]);
    $thumbid = explode("?f=videos&app=youtube_gdata", $thumburl1[1]);
    $thumbtitle = $thumbmedia->title;

    $thumbyt = $thumbmedia->children('http://gdata.youtube.com/schemas/2007')
        ->duration
    ;
    $thumblength = $thumbyt->attributes()->seconds;     
}

Ideas on if either of these are good solutions to my problem, and if so, how I can get over my execution humps? Thanks so much for any help you can give.

Update: Sample XML is here

A: 

I'm not precisely sure hwo to write to an array within a foreach loop and not write over one another

You can add elements to the end of an array with

$array[] = "new element";

or

array_push($array, "new element");

The first one is preferred, because it avoids a function call.

Artefacto
+2  A: 
foreach($thumbfeed->entry as $entry){
    $item = array();

    $thumbmedia = $entry->children('http://search.yahoo.com/mrss/')-&gt;group;
    $item['thumb'] = $thumbmedia->thumbnail[0]->attributes()->url;

    $thumburl = $thumbmedia->content[0]->attributes()->url;
    $thumburl1 = explode("http://www.youtube.com/v/", $thumburl[0]);
    $item['thumbid'] = explode("?f=videos&app=youtube_gdata", $thumburl1[1]);
    $item['thumbtitle'] = $thumbmedia->title;

    $thumbyt = $thumbmedia->children('http://gdata.youtube.com/schemas/2007')-&gt;duration;
    $item['thumblength'] = $thumbyt->attributes()->seconds;

    $items[] = $item;
}

The result will be an array $items[] which has each element being an associative array with the fields from the loop.

$x[] = $y; is a shorthand for "append $y as a new element to the array $x".

Amber
This is exactly what I needed; thanks, Amber! Last time I [asked a question](http://stackoverflow.com/questions/2862135/storing-multiple-inputs-with-the-same-name-in-a-codeigniter-session), it turned our I needed to add "[]" to the end of my input names. From now on, when in doubt, I'll simply add "[]" to everything and wait for the magic to happen.
Joshua Cody