note 81585 added to ref.simplexml
| From: | wyzzard at wp dot pl | Date: | Wed, 05 Mar 2008 00:33:23 +0000 |
| Subject: | note 81585 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136750@lists.php.net to get a copy of this message | ||
Consider following php code:
$xml=<<<HSPXML
<Hsp>
<Hsp_query-from>10</Hsp_query-from>
</Hsp>
HSPXML;
$Hsp = new SimpleXMLElement($xml);
//You can't simply access Hsp_query-form element because '-' is PHP's operator
var_dump($Hsp->Hsp_query-from);
var_dump($Hsp["Hsp_query-from"]);
var_dump(array_key_exists("Hsp_query-from" , $Hsp));
//Prints:
/*
int(0)
NULL
bool(true)
*/
//so the key exists! but is inaccessible with[] (and {})
//Way around is to create an additional array:
$hsp_array = array();
foreach($Hsp as $key=>$value)
{
$hsp_array[$key]=$value;
}
var_dump($hsp_array["Hsp_query-from"]);
//the proper result:
object(SimpleXMLElement)#3 (1) {
[0]=>
string(2) "10"
}
produced PHP 5.2.5, Windows, Wamp 2.0
----
Server IP: 82.160.94.10
Probable Submitter: 85.128.94.162
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- https://master.php.net/note/edit/81585
Del: integrated -- https://master.php.net/note/delete/81585/integrated
Del: useless -- https://master.php.net/note/delete/81585/useless
Del: bad code -- https://master.php.net/note/delete/81585/bad+code
Del: spam -- https://master.php.net/note/delete/81585/spam
Del: non-english -- https://master.php.net/note/delete/81585/non-english
Del: in docs -- https://master.php.net/note/delete/81585/in+docs
Del: other reasons-- https://master.php.net/note/delete/81585
Reject -- https://master.php.net/note/reject/81585
Search -- https://master.php.net/manage/user-notes.php