1 <?php
2 $root = simplexml_load_string('<root id="1"><node/></root>');
3
4 $array = array();
5 $array[$root['id']] = 'value';
6
7 $key = (int)$root['id'];
8 $array[$key] = 'value';
9 ?>
Warning: Illegal offset type in /home/sb/test.php on line 5
--
Sebastian Bergmann http://sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
Sebastian Bergmann wrote:
1 <?php
2 $root = simplexml_load_string('<root id="1"><node/></root>');
3
4 $array = array();
5 $array[$root['id']] = 'value';
6
7 $key = (int)$root['id'];
8 $array[$key] = 'value';
9 ?>
Warning: Illegal offset type in /home/sb/test.php on line 5
I've not used it much, but I believe you need to cast attributes coming
out of SimpleXML, in this case probably to an int.
-Stut
Stut wrote:
Sebastian Bergmann wrote:
1 <?php
2 $root = simplexml_load_string('<root id="1"><node/></root>');
3
4 $array = array();
5 $array[$root['id']] = 'value';
6
7 $key = (int)$root['id'];
8 $array[$key] = 'value';
9 ?>
Warning: Illegal offset type in /home/sb/test.php on line 5I've not used it much, but I believe you need to cast attributes coming
out of SimpleXML, in this case probably to an int.
that sounds about right. my experience with SimpleXML is that every is either
a string, an object, an array depending on how you are looking at it and regardless of
the situation auto-casting can be relied on to NOT do what you want/expect ...
the most problematic thing I found with simpleXML was the complete lack of means to
inspect an objects structure/content using funcs like var_dump()
[it seems
lots of __toString() magic lays waste to any attempt to look inside the object]
from what I gather the described 'annoyance' is indicative of the prescribed
SimpleXML behaviour.
I personally believe that SimpleXML is too clever and/or intuitive for it's own good
- or maybe I'm just incredibly stupid, either way I decided a while back to stick to
using the DOM extension for anything XML related because I found it so much easier to
use and understand.
I guess nothing in live is ever simple ;-)
-Stut
that sounds about right. my experience with SimpleXML is that every is
either
a string, an object, an array depending on how you are looking at it
and regardless of
the situation auto-casting can be relied on to NOT do what you
want/expect ...
the most problematic thing I found with simpleXML was the complete
lack of means to
inspect an objects structure/content using funcs likevar_dump()
[it
seems
lots of __toString() magic lays waste to any attempt to look inside
the object]from what I gather the described 'annoyance' is indicative of the
prescribed
SimpleXML behaviour.I personally believe that SimpleXML is too clever and/or intuitive for
it's own good
- or maybe I'm just incredibly stupid, either way I decided a while
back to stick to
using the DOM extension for anything XML related because I found it so
much easier to
use and understand.
+1
I couldn't figure anything out with SimpleXML because I could never
actually "see" what my data was. I'd ask it what it was, and it would
say it was an object, so I'd treat it like an object and it would
bitch at me so I had to pretend the object was an array and then
stumble down to the next node and start this infuriating process all
over again.
I sure hope somebody out there is getting something valuable out of
it, though, as it's obvious somebody went to a lot of effort to make
it do all those tricks.
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Richard Lynch wrote:
that sounds about right. my experience with SimpleXML is that every is
either
a string, an object, an array depending on how you are looking at it
and regardless of
the situation auto-casting can be relied on to NOT do what you
want/expect ...
the most problematic thing I found with simpleXML was the complete
lack of means to
inspect an objects structure/content using funcs likevar_dump()
[it
seems
lots of __toString() magic lays waste to any attempt to look inside
the object]from what I gather the described 'annoyance' is indicative of the
prescribed
SimpleXML behaviour.I personally believe that SimpleXML is too clever and/or intuitive for
it's own good
- or maybe I'm just incredibly stupid, either way I decided a while
back to stick to
using the DOM extension for anything XML related because I found it so
much easier to
use and understand.+1
I couldn't figure anything out with SimpleXML because I could never
actually "see" what my data was. I'd ask it what it was, and it would
say it was an object, so I'd treat it like an object and it would
bitch at me so I had to pretend the object was an array and then
stumble down to the next node and start this infuriating process all
over again.I sure hope somebody out there is getting something valuable out of
it, though, as it's obvious somebody went to a lot of effort to make
it do all those tricks.
The var_dump/print_r support was fixed a while ago. It's quite easy to
see what is in a SimpleXML object. The only time it gets a little bit
tricky is when your source is heavily namespaced. Then you need to
explicitly state which namespace to get things from. But, take for
example PHP's RSS feed at php.net/news.rss:
$xml = simplexml_load_file('news.rss');
print_r($xml);
The top of which gives you (with a couple of namespaced entries removed):
SimpleXMLElement Object
(
[channel] => SimpleXMLElement Object
(
[title] => PHP: Hypertext Preprocessor
[link] => http://www.php.net/
[description] => The PHP scripting language web site
)
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => PHP 5.2.0 Released
[link] => http://www.php.net/releases/5_2_0.php
[description] => The PHP development team is proud
to announce the immediate release of PHP 5.2.0. This release is a major
improvement in the 5.X series, which includes a large number of new
features, bug fixes and security enhancements. Further details about
this release can be found in the release announcement 5.2.0, the full
list of changes is available in the ChangeLog PHP 5. All users of PHP,
especially those using earlier PHP 5 releases are advised to upgrade to
this release as soon as possible. This release also obsoletes the 5.1
branch of PHP. For users upgrading from PHP 5.0 and PHP 5.1 there is an
upgrading guide available here, detailing the changes between those
releases and PHP 5.2.0.
)
...
From that I think it is quite obvious that a simple:
foreach(simplexml_load_file('http://www.php.net/news.rss')->item as $item) {
echo <<<EOB
Gives you a very quick and simple way to put up a list of the items in
that feed. That's what SimpleXML is for. And this is what a lot of
people find extremely useful. If you want to fiddle with namespaces, it
does support it via the getNamespaces() and children(namespace) methods,
but then it starts to become rather non-simple.
-Rasmus