2010-05-31 24 views
2

我的問題癥結所在: 我有一個XML file,返回20個結果。在這些結果中是我需要獲得的所有元素。現在,我需要以隨機順序返回它們,並能夠專門處理項目1,項目2-5和項目6-17。如何將SimpleXML對象轉換爲數組,然後洗牌?

想法1:使用this script將對象轉換爲一個數組,我可以將其拖動通過。這接近工作,但我需要獲得的一些元素在不同的命名空間下,而我似乎無法獲得它們。代碼:

/* 
* 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; 
} 

理念2:繼續使用越來越使用foreach的信息我工作的代碼,但每個元素存儲在數組中,然後使用洗牌上。不過,我並不確定如何在foreach循環中寫入數組,而不是寫入另一個數組。工作代碼:

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;  
} 

思路上如果這些都是我的問題很好的解決方案,如果是這樣,我怎麼能得到超過我的執行駝峯?非常感謝您提供的任何幫助。

更新:示例XML is here

+0

如果您不知道如何獲取具有名稱空間的元素,請參閱以下教程:http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/ if你會提供一些示例XML,我們可能會幫助你更好。 – 2ndkauboy 2010-05-31 22:37:42

回答

2
foreach($thumbfeed->entry as $entry){ 
    $item = array(); 

    $thumbmedia = $entry->children('http://search.yahoo.com/mrss/')->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')->duration; 
    $item['thumblength'] = $thumbyt->attributes()->seconds; 

    $items[] = $item; 
} 

其結果將是,其具有每一個元素是一個關聯數組與從環路中的字段的陣列$items[]

$x[] = $y;是「附加$y作爲數組$x的新元素」的簡寫。

+0

這正是我所需要的;謝謝,琥珀!上次我[問一個問題](http://stackoverflow.com/questions/2862135/storing-multiple-inputs-with-the-same-name-in-a-codeigniter-session),它把我們需要的將「[]」添加到我的輸入名稱的末尾。從現在開始,如果有疑問,我會簡單地在所有內容中加上「[]」並等待魔法發生。 – 2010-05-31 22:51:32

0

我不能準確地確定藿寫入到陣列中的foreach循環中,而不是在一個再寫

您可以添加元素陣列末尾有

$array[] = "new element"; 

array_push($array, "new element"); 

第一個是首選,因爲它避免了函數調用。

相關問題