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 を実行したいと思います。助言がありますか?私は立ち往生しています.これは、これらの結果を得るために使用したコードです:

$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>";
4

1 に答える 1

1

の配列がSimpleXMLElementあるようですので、配列を反復処理して機能を使用できますSimpleXMLElement

試す:

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

その他の質問については、ドキュメントをご覧ください: SimpleXMLElement

key特定の問題については、とが同期されていると仮定してstring、次を試すことができます。

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

として

[3] => Album Artist

ポジション3を与える

次に、文字列値を取得すると、戻りますFrankie Sullivan/Jim Peterik

于 2013-03-29T04:46:05.647 に答える