cppit
cppit

Reputation: 4564

get specific values of array not showing

This is what my array looks like :

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 ) )

that's 1 result, there is about 50 of them repeated.

I am trying to select the artist value in this case : Frankie Sullivan/Jim Peterik please note: there is about 50 other results that come after this first one, so I would like to do a foreach to display all results. any suggestions? I am stuck.this is the code I used to get these results:

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

Upvotes: 0

Views: 54

Answers (1)

MatRt
MatRt

Reputation: 3534

It seems that you have an array of SimpleXMLElement, so you can iterate over your array and use the SimpleXMLElement facilities.

Try:

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

Take a look at the documentation for more question: SimpleXMLElement

For your specific problem, assuming the key and string are synchronized, you can try:

// 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

will give the position 3

Then, when retrieving the string value, it will return Frankie Sullivan/Jim Peterik

Upvotes: 1

Related Questions