2013-03-29 48 views
0

這是我的數組類似:得到陣列的具體數值沒有顯示

Array ( 
    [0] => SimpleXMLElement Object ( 
     [key] => Array ( 
      [0] => Track ID 
      [1] => Name 
      [2] => Artist 
      [3] => Album Artist 
      [4] => Composer 
      [5] => Album 
      [6] => Genre 
      [7] => Kind 
      [8] => Size 
      [9] => Total Time 
      [10] => Disc Number 
      [11] => Disc Count 
      [12] => Track Number 
      [13] => Year 
      [14] => Date Modified 
      [15] => Date Added 
      [16] => Bit Rate 
      [17] => Sample Rate 
      [18] => Play Count 
      [19] => Play Date 
      [20] => Play Date UTC 
      [21] => Artwork Count 
      [22] => Persistent ID 
      [23] => Track Type 
      [24] => Location 
      [25] => File Folder Count 
      [26] => Library Folder Count) 
     [integer] => Array ( 
      [0] => 2056 
      [1] => 3732918 
      [2] => 230661 
      [3] => 1 
      [4] => 1 
      [5] => 1 
      [6] => 1993 
      [7] => 128 
      [8] => 44100 
      [9] => 3 
      [10] => 3439412487 
      [11] => 1 
      [12] => 5 
      [13] => 1) 
     [string] => Array ( 
      [0] => Eye of the Tiger 
      [1] => Survivor 
      [2] => Survivor 
      [3] => Frankie Sullivan/Jim Peterik 
      [4] => Greatest Hits 
      [5] => Rock 
      [6] => MPEG audio file 
      [7] => 772F0F53F195E705 
      [8] => File 
      [9] => file://localhost/Users/cameron/Music/iTunes/iTunes%20Media/Music/Survivor/Greatest%20Hits/01%20Eye%20of%20the%20Tiger.mp3) 
     [date] => Array ( 
      [0] => 2012-08-27T17:01:00Z 
      [1] => 2012-08-27T17:01:03Z 
      [2] => 2012-12-27T07:21:27Z)) 

這是1名的結果,存在重複對其的約50。

我想在這種情況下選擇藝術家的價值:Frankie Sullivan/Jim Peterik 請注意:在第一個之後還有大約50個其他結果,所以我想要做一個foreach來顯示所有結果。 有什麼建議嗎?我stuck.this是我用來獲取這些結果的代碼:

$xml = simplexml_load_file('Library.xml'); 
$xmlx = $xml->dict->dict->dict->key; 
$artist = $xmlx->xpath("//dict[key='Artist']"); 

echo "<pre>" . print_r($artist, true) . "</pre>"; 
+0

使用'回聲「

" . print_r($yourObject, true) . "
」;',它粘貼在這裏 - 有一個更好的人類可讀的內容。你有什麼嘗試。? – MatRt

+0

你去了。 @ hjpotter92謝謝 – cppit

+0

你能粘貼atleast 2結果嗎? –

回答

1

看來你有SimpleXMLElement數組, 這樣你就可以遍歷陣列,並且使用SimpleXMLElement設施。

嘗試:

foreach($yourArray as $simpleXmlElement) 
{ 
    // Retrieve the name 
    echo $simpleXmlElement->string[3]; 
} 

看看更多的問題的文檔:SimpleXMLElement

爲了您的具體問題,假設keystring是同步的,你可以嘗試:

// Loop on your array of SimpleXMLElement 
foreach($yourArray as $simpleXmlElement) 
{ 
    // Initialize the position and found value 
    $position = 0; 
    $found = false; 

    // Loop on the sub array 'key' and search the 'Artist Album' position 
    foreach($simpleXmlElement->key as $index => $value) 
    { 
     if ($value == "Album Artist") 
     { 
      $found = true; 
      break; 
     } 

     // Not found, increment position 
     $position++; 
    } 

    // Retrieve the name if found the artist album key 
    if ($found) 
     echo $simpleXmlElement->string[$position]; 
} 

As

[3] => Album Artist 

將給位置3

然後,檢索字符串值時,它會返回Frankie Sullivan/Jim Peterik

+0

我可以訪問密鑰「藝術家」,然後顯示字符串值而不是字符串[3]?因爲它移動並顯示錯誤的結果 – cppit

+0

由於你的數組不包含這樣的索引,所以很難說。如果'key'和'string'是同步的,那麼你可以迭代'key',並且當你找到''Artist''時,在同一個位置檢索'string'條目。在我的文章中添加代碼 – MatRt

+0

您的文章中的代碼就像我說的那樣顯示錯誤的結果,這就是爲什麼我要遍歷鍵......甚至使用正則表達式(如果可行)。你發佈的代碼在每次迭代中移動1個位置......它無法工作,不幸的是 – cppit