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

对于您的具体问题,假设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];
}

作为

[3] => Album Artist

将给出位置 3

然后,在检索字符串值时,它会返回Frankie Sullivan/Jim Peterik

于 2013-03-29T04:46:05.647 回答