How can I turn a SimpleXML object to array, then shuffle?
- by Joshua Cody
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 hwo 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.