I have recently been working with tv_grab_uk_rt which generates a TV guide xml file. I have written a script to convert the XML into an object which I can the loop through and insert into a database. Whilst I have the script working, I came across an issue I was looking to get clarification for.
When putting the XML to an object I get the following:
SimpleXMLElement Object
(
[@attributes] => Array
(
[date] => Mon, 23 Dec 2013 04:30:01 GMT
[source-info-url] => http://www.radiotimes.com
[source-info-name] => Radio Times XMLTV Service
[source-data-url] => http://xmltv.radiotimes.com/xmltv/channels.dat
[generator-info-name] => XMLTV/0.5.61, tv_grab_uk_rt 1.342, 2011/06/19 06:50:36
[generator-info-url] => http://www.xmltv.org
)
[channel] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => fiver.channel5.co.uk
)
[display-name] => 5*
[icon] => SimpleXMLElement Object
(
[@attributes] => Array
(
[src] => http://www.lyngsat-logo.com/logo/tv/cc/channel5_star.jpg
)
)
)
)
)
Lets say this object is contained within the variable $xml
, if I were to do the following:
foreach($xml->channel as $channel)
{
echo $channel->displayname
}
I realise I would be able to echo the object property of displayname
, in this case 5*
.
But what happens if I wanted to say echo the src
in this case http://www.lyngsat-logo.com/logo/tv/cc/channel5_star.jpg
, how would I go about doing this with an object. I can't for example do
foreach($xml->channel as $channel)
{
echo $channel->icon->@attributes->src
}
With arrays for example you could easily do
foreach($xml['channel'] as $channel)
{
echo $channel['icon']['@attributes']['src'];
}
But not with objects. Rather than getting into endless loops I found I could convert the object to an array like so
foreach($xml->channel as $channel)
{
echo $channel['icon']['@attributes']['src'];
$channelArray = get_object_vars($channel);
}
Then I can simply access the properties as an array. So my question really is, without converting the object into an array, is there a way to drill into the properties ie
$xml->channel->0->displayname