simplexml_debug icon indicating copy to clipboard operation
simplexml_debug copied to clipboard

Can't distinguish single nodes from lists with one item

Open IMSoP opened this issue 12 years ago • 2 comments

There is currently no way of distinguishing between a single element (e.g. $sxml->Result[0]) and a list of elements (e.g. $sxml->Result or $sxml->children()) which happens to include only one item. They can however behave differently - foreach ( $sxml->Result[0] as $result ) is equivalent to foreach ( $sxml->Result[0]->children() as $result ).

As far as I can see, there is no way of determining this with the current API. However, since the behaviour is different, the extension itself clearly tracks this distinction. It should probably be raised with the maintainers to add PHP access to this property for a future release.

IMSoP avatar Sep 05 '12 21:09 IMSoP

hakre has some interesting tricks for spotting different object types here: http://hakre.wordpress.com/2013/02/12/simplexml-type-cheatsheet/

IMSoP avatar May 02 '13 21:05 IMSoP

Haha, just was stumbling over your README because I linked to the project in a recent SO answer and then stumbled over that and thought: mhm, that should be answered. And yes it is, I've also created an example here as a gist.

Execute it on 3v4l.org, results were:

Output for 5.2.2 - 7.0.0beta3, hhvm-3.3.1 - 3.8.1

For the single element:
  Is single-element?   - Yes
  Is list-of-elements? - No

For the list of elements with a single item:
  Is single-element?   - No
  Is list-of-elements? - Yes

Do not use it with a PHP version before 5.2.2.

hakre avatar Aug 09 '15 14:08 hakre