9841

Accessing Object Properties (Drilling down without array conversion) [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/1652128/accessing-attribute-from-simplexml" dir="ltr" rel="nofollow">Accessing @attribute from SimpleXML</a> <span class="question-originals-answer-count"> 8 answers </span> </li> </ul>

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

Answer1:

Each channel is a <a href="http://www.php.net/manual/en/class.simplexmlelement.php" rel="nofollow">SimpleXMLElement</a> instance so you can

foreach($xml->channel as $channel) { echo $channel->icon->attributes()->src; }

When in doubt always start from the official docs.

Recommend

  • Putting into table from a complicated array using php
  • Get an xml object by attribute
  • Parse RDF XML file to get all rdf:about values
  • Consistent Client Side Date/timestamp using JavaScript(considering TimeZones)
  • Gracefull shutdown for Spring boot Application
  • R Error - cannot change value of locked binding for 'df'
  • x86: Count transitions from 1 to 0 in 32 bit number
  • Searching an XML file using PHP [closed]
  • How can you tell if a Python program has anything to read from stdin?
  • toInstant() in Calendar is showing in GMT instead of Local time
  • Convert unix time to week day
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • how to get data attributes of dynamically generated element
  • rewrite uppercase url to lowercase url htaccess
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • ListItem.Attributes.Add not working
  • How to change the font size of a single index for UISegmentedControl?
  • How to rebase a series of branches?
  • Illegal mix of collations for operation for date/time comparison
  • Counter field in MS Access, how to generate?
  • Javascript + PHP Encryption with pidCrypt
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • Circular dependency while pushing http interceptor
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • Change div Background jquery
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?