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.
The items you are adding to the array are
SimpleXMLElements. If you just want to add the string value, you must cast theSimpleXMLElementto a string.In general, you always want to cast
SimpleXMLElementto a string when you want the string value. In some cases PHP will automatically coerce to string (for example when youechoit), 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.)