PHP uses simplexml to solve the illegal offset type Problem
Simplexml_load_string/simplexml_load_file of PHP is very convenient. You can easily convert XML Parse into a PHP Object, and then you can process the information as needed ~
- Simplexml_load_string: Export XML.
- Simplexml_load_file: Upload URL. It is very convenient to get Parse XML/RSS feed.
Do
The processing of the ticket is not very common, but now it is necessary to convert the object value into a general PHP array key-> Value
Type problem. I don't know how to use it, but I can't find the correct solution. So I just picked it up and tried again. (If you want to have a positive solution, please refer to the solution again .. orz)
Protocol: PHP version: PhP 5.2.3
Let's take a look at a simple program example (original program ):
<? Php <br/> $ DATA = <EOF <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <response> <br/> <status> 0 </status> <br/> <result> <br/> <Doc> <br/> <STR name = "title"> title value </STR> <br/> <STR name = "Description"> description value </STR> <br/> </DOC> <br/> </result> <br/> </response> <br/> EOF; <br/> $ xml = simplexml_load_string ($ data); <br/> $ Results = array (); <br/> foreach ($ XML-> result-> doc as $ story) {<br/> $ xmlarray = array (); <br/> try {<br/> foreach ($ story as $ ite M) {<br/> $ name = $ item-> attributes ()-> name; <br/> $ value = $ item-> attributes () -> {0 }; <br/> $ xmlarray [$ name] = $ value; // have "illegal offset type" error. <br/>}< br/>} catch (exception $ e) {<br/> echo 'problem handling XML array. '; <br/>}< br/> print_r ($ xmlarray); <br/>}< br/>?>
The purpose of this program is to extract the XML of $ data, and then parse it into $ xmlarray, and specify that the key of $ xmlarray is the value of tag attribute name, the value is the value from the name tag package, or you can directly view the expected response line as follows:
Array <br/> (<br/> [title] => title <br/> [description] => description <br/>)
However, the result of the explain row is that $ xmlarray is empty, mainly because the following warning occurs:
Warning: Illegal offset type in XML. php on line 24
Then, var_dump ($ name) shows the structure of the object as follows:
Object (simplexmlelement) #9 (1) {<br/> [0] => <br/> string (5) "title" <br/>}
Very
Obviously, it is necessary to access the [0] of the object, but I found that my $ name [0], $ name-> {0 },
That is, there is no way to simply pull the value out (using $ name [0], $ name-> {0} And var_dump () are the same as above ),
In addition, they all appear.Illegal offset type
Question.
Then, when I look for the web, I seem to have no idea how to explain it (or maybe I am looking for a poor capability orz, later, you can seeThis article (I want to say that the model has never been used, so I will try again )......... start to do evil tricks ~
As a result, I made strval () for $ name. I didn't think it would be a success ~
Therefore, the program changes the syntax as follows ($ data is not changed, only foreach is modified at the bottom, and strval () has been used first ):
<? Php <br/> $ DATA = <EOF <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <response> <br/> <status> 0 </status> <br/> <result> <br/> <Doc> <br/> <STR name = "title"> title value </STR> <br/> <STR name = "Description"> description value </STR> <br/> </DOC> <br/> </result> <br/> </response> <br/> EOF; <br/> $ xml = simplexml_load_string ($ data); <br/> $ Results = array (); <br/> foreach ($ XML-> result-> doc as $ story) {<br/> $ xmlarray = array (); <br/> try {<br/> foreach ($ story as $ ite M) {<br/> $ name = $ item-> attributes ()-> name; <br/> $ value = $ item-> attributes () -> {0 }; <br/> $ name = strval ($ name ); // $ name/$ name-> {0} is used in this example. <br/> $ value = strval ($ value ); // $ value/$ value-> {0} is used in this example. <br/> $ xmlarray [$ name] = $ value; <br/>}< br/>} catch (exception $ e) {<br/> echo 'problem handling XML array. '; <br/>}< br/> print_r ($ xmlarray); <br/>}< br/>?>
In this way, $ xmlarray is the result of our request after the sub-statement row ~
Inductive lp81sam provides the following positive solutions:
<? Php <br/> $ DATA = <EOF <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <response> <br/> <status> 0 </status> <br/> <result> <br/> <Doc> <br/> <STR name = "title"> title value </STR> <br/> <STR name = "Description"> description value </STR> <br/> </DOC> <br/> </result> <br/> </response> <br/> EOF; <br/> $ xml = simplexml_load_string ($ data); <br/> $ Results = array (); <br/> foreach ($ XML-> result-> doc as $ story) {<br/> $ xmlarray = array (); <br/> try {<br/> foreach ($ story as $ ite M) {<br/> $ name = $ item-> getattribute ('name'); // use getattribute () <br/> $ value = $ item-> getattribute (0); <br/>}< br/>} catch (exception $ E) {<br/> echo 'problem handling XML array. '; <br/>}< br/> print_r ($ xmlarray); <br/>}< br/>?>
The getattribute () internal operation mode is roughly as follows:
public function getAttribute($name){<br /> foreach($this->attributes() as $key=>$val){<br /> if($key == $name){<br /> return (string)$val;<br /> }// end if<br /> }// end foreach<br />}// end function getAttribute
Therefore, you can use getattribute () to capture values or run foreach.
Bytes ------------------------------------------------------------------------------------------------------------
As it turns out, I understand... it's mainly getattribute ().
The internal operation of getattribute () is:
Function getattribute ($ name ){
Foreach ($ this-> attributes () as $ key => $ Val ){
}
}
Therefore, you can use foreach ($ this-> attributes () as $ key => $ Val) to pull out each value and then handle it by yourself.
Or you can use getattribute () to pull the value directly ~~~