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: this script I ile karıştırmak için bir dizi nesneyi dönüştürmek için kullanın. Bu çalışma yakın, ama ben almak gerekir elemanlarının birkaç farklı bir ad altında, ve ben onları almak mümkün görünmüyor. Kod:
/*
* 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:, bir foreach kullanarak bilgiyi oluyor benim çalışma kodu kullanarak devam edin, ancak bir dizideki her öğe saklamak, bir o shuffle kullanın. Ben bir foreach döngüsü içinde bir dizi yazmak ve olsa da, birbiri üzerine yazmak değil nasıl tam olarak emin değilim. Çalışma kodu:
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;
}
Ya bu benim soruna iyi bir çözüm vardır ve eğer öyleyse, nasıl benim yürütme hörgüçlü üzerinden alabilirsiniz üzerinde fikir? Çok teşekkürler herhangi bir yardım için verebilirsiniz.
Update: Örnek XML is here