Simplexml_load_string converting XML strings to Strings
$string = <<
Forty what?
Joe
Jane
I know that ' s the answer--and what's the ' s the question?< c15/>
XML; $xml = simplexml_load_string ($string); Var_dump ($xml);
The above code will output:
SimpleXMLElement Object ( [title] = Forty What? [from] + Joe [to] + Jane [body] = I know that's the answer--but what ' s the question? )
If you want to convert the XML to an array, convert it to a string that contains a key value, and then loop it to be a list:
/** * XML conversion to an array * @param unknown_type $xml */Private Function Xml_to_array ($xml) { $array = (array) (Simplexml_load_string ($xml, ' simplexmlelement ', libxml_nocdata)); foreach ($array as $key = + $item) { $array [$key] = $this->struct_to_array ((array) $item); } return $array;} Private Function Struct_to_array ($item) { if (!is_string ($item)) { $item = (array) $item; foreach ($item as $key = + $val) { $item [$key] = Self::struct_to_array ($val);} } return $item;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.