0

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>";
5
  • use echo "<pre>" . print_r($yourObject, true) . "</pre>"; and paste it here - to have a better human readable content. And what have you try .? Commented Mar 29, 2013 at 4:35
  • there you go. @hjpotter92 thank you Commented Mar 29, 2013 at 4:38
  • can you paste atleast 2 results? Commented Mar 29, 2013 at 4:43
  • could you show the code that you wrote to get the resulting data..? Commented Mar 29, 2013 at 4:45
  • Can you paste a sample content of Library.xml? Probably, you are wanting to loop the list of Library.xml. Commented Mar 29, 2013 at 4:51

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

can I access the key "artist" then display the string value instead of string[3] ? because it shifts and displays the wrong results
As your array does not contain such index, it difficult to say. If key and string are synchronized, then you can iterate over key, and when you find "Artist", retrieve the string entry at the same position. Add the code in my post
the code in your post like I said shows the wrong results that's why I want to iterate over key ...even use regular expressions if doable. the code you posted shifts 1 position on each iteration .... it didnt work unfortunately
You want to extract Frankie Sullivan/Jim Peterik and my second code do the job. Don't understand what do you expect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.