dreamwest Posted December 20, 2010 Share Posted December 20, 2010 Im trying to parse this xml but i can get it to output a simple string, can someone see what im doing wrong?? $str = "<channel><item> <id>cb-uA4QHmpDxYdpgW1jPwiYsUwjBViwlinS</id> <title>Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12</title> </item> </channel>"; $n_data = new SimpleXmlElement($str, LIBXML_NOCDATA); foreach ($n_data->channel->item as $d) { $vid = $d->id; $title = $d->title; print_r($d->title); //testing die(); //testing $arr2[] = array('vid' => $vid, 'title' => $title, 'site_id' => 10 ,'time' => 456); } The print_r() returns SimpleXMLElement Object ( [0] => Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12 ) If i simply echo $d->title it returns a string, but i need to reapply $d->title to another array as shown above Quote Link to comment Share on other sites More sharing options...
Adam Posted December 20, 2010 Share Posted December 20, 2010 As the print_r() shows, the string is stored in an index. Try: $title = $d->title[0]; Quote Link to comment Share on other sites More sharing options...
dreamwest Posted December 20, 2010 Author Share Posted December 20, 2010 Actually i thought that worked but the problem is $title in the new array get the print_r($title) SimpleXMLElement Object ( [0] => Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12 )} print_r($arr2); Array ( [0] => Array ( [vid] => SimpleXMLElement Object ( [0] => cb-uA4QHmpDxYdpgW1jPwiYsUwjBViwlinS ) [title] => SimpleXMLElement Object ( [0] => Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12 ) [site_id] => 10 [time] => 456 ) [1] => Array ( [vid] => SimpleXMLElement Object ( [0] => cb-pOEUN4deIJ2cc_22ZjFCaCIcgimN6D2A ) [title] => SimpleXMLElement Object ( [0] => David Letterman - Matt Damon: Tongue-Tied - Season 18 - Episode 3415 ) [site_id] => 10 [time] => 456 ) ) Quote Link to comment Share on other sites More sharing options...
salathe Posted December 20, 2010 Share Posted December 20, 2010 Your main problem is using $n_data->channel->item where you want to be using $n_data->item. When you construct a new SimpleXMLElement instance like you're doing, the document's root node (<channel>) is what that instance represents. You'll also probably want to cast the values to strings when you put them into the $arr2 array. Quote Link to comment 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.