2

Okey so I am a bit confused about this. I have a xml that looks something like this:

<track>
<artist mbid="77cceea7-91bb-4a4c-ae41-bc9c46c1ccb5"> Red Hot Chili Peppers </artist>
<name> under the bridge </name>
<streamable>0</streamable>
<mbid/>
<album mbid="0fe94139-df63-4e51-b2e7-a1d53535cdd9">  Blood Sugar Sex Magik </album>
<url> http://xxxxxx.com </url>
<date uts="1351691244">31 Oct 2012, 13:47</date>
</track>

And I use simpleXML to parse the xml like this:

$artists = array();

$xml = simplexml_load_file("http://xxxxxxxxxxxxxx");

foreach($xml->recenttracks->track  as $track)
{
$artist = $track->artist;
    array_push($artists, $artist);
}  

var_dump($artists);

now I was hoping to get a nice array looking like this:

array(4) {
[0]=>
string(20) "Red Hot Chili Peppers "
[1]=>
string(20) "Red Hot Chili Peppers"
}

but what I am getting is something like this:

array(2) 
{ 
[0]=> object(SimpleXMLElement)#6 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
[1]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(1) { ["mbid"]=> string(36) "8bfac288-ccc5-448d-9573-c33ea2aa5c30" } [0]=> string(21) "Red Hot Chili Peppers" } 
} 

Now how do I get the artist only, not the whole SimpleXMLElement because I just can't figure it out.

4 Answers 4

5

The items you are adding to the array are SimpleXMLElements. If you just want to add the string value, you must cast the SimpleXMLElement to a string.

$artists = array();
foreach($xml->recenttracks->track  as $track)
{
    $artists[] = (string) $track->artist;
}  

var_export($artists);

In general, you always want to cast SimpleXMLElement to a string when you want the string value. In some cases PHP will automatically coerce to string (for example when you echo it), but PHP type coercion rules are so complicated that it's better to always be explicit.

(Also, there is no need for array_push(), just use the bracket notation $arrayname[] = $appendedvalue.)

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

Comments

1

The var_dump is giving you keys that you can use to access the different pieces, and casting to a string will give you just the node value, try:

$artist = (string) $track->artist[0];

Comments

0

Have a look at these links... they've helped me in the past.

http://www.bookofzeus.com/articles/convert-simplexml-object-into-php-array/

http://milesj.me/blog/read/simplexml-to-array

Comments

0

Try the below code.

$xml = simplexml_load_file("artist.xml");

echo $xml->artist;

2 Comments

if I just echo the artist I get the artist name only but if i push it in to the array I get all this extra information. forgot to mention that in the description.
@user1593846 - the confusion is that you didn't post the complete XML document, just one node. See my answer for how to access the value.

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.