note 81585 deleted from ref.simplexml by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:14:15 +0000 |
| Subject: | note 81585 deleted from ref.simplexml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155497@lists.php.net to get a copy of this message | ||
Note Submitter: wyzzard at wp dot pl
----
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