lemmin Posted July 16, 2012 Share Posted July 16, 2012 Either this output is wrong or I am losing my mind: $f = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=track.getsimilar&artist=Astronautalis&track=The+Wondersmith+and+His+Sons&limit=3&api_key=b25b959554ed76058ac220b7b2e0a026'); $similar = simplexml_load_string($f); print_r($similar); print_r($similar->similartracks); print_r($similar->similartracks->track); Output: SimpleXMLElement Object ( [@attributes] => Array ( [status] => ok ) [similartracks] => SimpleXMLElement Object ( [@attributes] => Array ( [track] => The Wondersmith and His Sons [artist] => Astronautalis ) [track] => Array ( [0] => SimpleXMLElement Object ( [name] => Secrets of the Undersea Bell [playcount] => 55141 [mbid] => 0b3918ae-f4fc-4312-a185-640dd07b8086 [match] => 1 [url] => http://www.last.fm/music/Astronautalis/_/Secrets+of+the+Undersea+Bell [streamable] => 1 [duration] => 214000 [artist] => SimpleXMLElement Object ( [name] => Astronautalis [mbid] => aa79fa15-c1fe-4114-99ba-1f55d99fceaa [url] => http://www.last.fm/music/Astronautalis ) [image] => Array ( [0] => http://userserve-ak.last.fm/serve/34s/52906489.jpg [1] => http://userserve-ak.last.fm/serve/64s/52906489.jpg [2] => http://userserve-ak.last.fm/serve/126/52906489.jpg [3] => http://userserve-ak.last.fm/serve/300x300/52906489.jpg ) ) [1] => SimpleXMLElement Object ( [name] => My Old Man's Badge [playcount] => 45879 [mbid] => d90368b1-144d-4021-96d2-aa840d12d506 [match] => 0.964277 [url] => http://www.last.fm/music/Astronautalis/_/My+Old+Man's+Badge [streamable] => 1 [duration] => 214000 [artist] => SimpleXMLElement Object ( [name] => Astronautalis [mbid] => aa79fa15-c1fe-4114-99ba-1f55d99fceaa [url] => http://www.last.fm/music/Astronautalis ) [image] => Array ( [0] => http://userserve-ak.last.fm/serve/34s/52906489.jpg [1] => http://userserve-ak.last.fm/serve/64s/52906489.jpg [2] => http://userserve-ak.last.fm/serve/126/52906489.jpg [3] => http://userserve-ak.last.fm/serve/300x300/52906489.jpg ) ) [2] => SimpleXMLElement Object ( [name] => A. Letec [playcount] => 3760 [mbid] => 3b1450f7-9b5e-489c-baaa-1a7b356ed7ae [match] => 0.635671 [url] => http://www.last.fm/music/OTK/_/A.+Letec [streamable] => 1 [duration] => 214000 [artist] => SimpleXMLElement Object ( [name] => OTK [mbid] => c8bfbcf9-1208-41ce-9d5e-8188240249a0 [url] => http://www.last.fm/music/OTK ) ) ) ) ) SimpleXMLElement Object ( [@attributes] => Array ( [track] => The Wondersmith and His Sons [artist] => Astronautalis ) [track] => Array ( [0] => SimpleXMLElement Object ( [name] => Secrets of the Undersea Bell [playcount] => 55141 [mbid] => 0b3918ae-f4fc-4312-a185-640dd07b8086 [match] => 1 [url] => http://www.last.fm/music/Astronautalis/_/Secrets+of+the+Undersea+Bell [streamable] => 1 [duration] => 214000 [artist] => SimpleXMLElement Object ( [name] => Astronautalis [mbid] => aa79fa15-c1fe-4114-99ba-1f55d99fceaa [url] => http://www.last.fm/music/Astronautalis ) [image] => Array ( [0] => http://userserve-ak.last.fm/serve/34s/52906489.jpg [1] => http://userserve-ak.last.fm/serve/64s/52906489.jpg [2] => http://userserve-ak.last.fm/serve/126/52906489.jpg [3] => http://userserve-ak.last.fm/serve/300x300/52906489.jpg ) ) [1] => SimpleXMLElement Object ( [name] => My Old Man's Badge [playcount] => 45879 [mbid] => d90368b1-144d-4021-96d2-aa840d12d506 [match] => 0.964277 [url] => http://www.last.fm/music/Astronautalis/_/My+Old+Man's+Badge [streamable] => 1 [duration] => 214000 [artist] => SimpleXMLElement Object ( [name] => Astronautalis [mbid] => aa79fa15-c1fe-4114-99ba-1f55d99fceaa [url] => http://www.last.fm/music/Astronautalis ) [image] => Array ( [0] => http://userserve-ak.last.fm/serve/34s/52906489.jpg [1] => http://userserve-ak.last.fm/serve/64s/52906489.jpg [2] => http://userserve-ak.last.fm/serve/126/52906489.jpg [3] => http://userserve-ak.last.fm/serve/300x300/52906489.jpg ) ) [2] => SimpleXMLElement Object ( [name] => A. Letec [playcount] => 3760 [mbid] => 3b1450f7-9b5e-489c-baaa-1a7b356ed7ae [match] => 0.635671 [url] => http://www.last.fm/music/OTK/_/A.+Letec [streamable] => 1 [duration] => 214000 [artist] => SimpleXMLElement Object ( [name] => OTK [mbid] => c8bfbcf9-1208-41ce-9d5e-8188240249a0 [url] => http://www.last.fm/music/OTK ) ) ) ) SimpleXMLElement Object ( [name] => Secrets of the Undersea Bell [playcount] => 55141 [mbid] => 0b3918ae-f4fc-4312-a185-640dd07b8086 [match] => 1 [url] => http://www.last.fm/music/Astronautalis/_/Secrets+of+the+Undersea+Bell [streamable] => 1 [duration] => 214000 [artist] => SimpleXMLElement Object ( [name] => Astronautalis [mbid] => aa79fa15-c1fe-4114-99ba-1f55d99fceaa [url] => http://www.last.fm/music/Astronautalis ) [image] => Array ( [0] => http://userserve-ak.last.fm/serve/34s/52906489.jpg [1] => http://userserve-ak.last.fm/serve/64s/52906489.jpg [2] => http://userserve-ak.last.fm/serve/126/52906489.jpg [3] => http://userserve-ak.last.fm/serve/300x300/52906489.jpg ) ) The first two print_r's show the track variable as an array. When I print it, it shows the first element in that array, not the array itself. If I pass that variable to an array function (array_shift or something) it throws a warning because it isn't an array. Why does it say it is an array?! I can work around this, but I'm just really confused here. Quote Link to comment https://forums.phpfreaks.com/topic/265766-simplexml-object-unexpected-type/ Share on other sites More sharing options...
ignace Posted July 16, 2012 Share Posted July 16, 2012 $url = 'http://ws.audioscrobbler.com/2.0/?method=track.getsimilar&artist=Astronautalis&track=The+Wondersmith+and+His+Sons&limit=3&api_key=b25b959554ed76058ac220b7b2e0a026'; $array = json_decode(json_encode(new SimpleXmlElement($url, 0, true)), true); Quote Link to comment https://forums.phpfreaks.com/topic/265766-simplexml-object-unexpected-type/#findComment-1361933 Share on other sites More sharing options...
Barand Posted July 16, 2012 Share Posted July 16, 2012 or just cast it as an array $x = (array)$similar->similartracks->track; // cast as array echo '<pre>'.print_r(array_pop($x), 1).'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/265766-simplexml-object-unexpected-type/#findComment-1361943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.