如何从php中包含冒号的数组访问对象

时间:2012-07-07 09:25:46

标签: php web

  

可能重复:
  PHP: Accessing namespaced XML with SimpleXML

解析后

无法从此数组访问该对象
<abc>
<ls:location>
      <ls:address1>No 16 GLama</ls:address1>
      <ls:city>Kuala Lumpur</ls:city>
      <ls:zip>58200</ls:zip>
      <ls:latitude>3.092055</ls:latitude>
      <ls:longitude>101.684757</ls:longitude>
</ls:location>
</abc>


如何访问zip对象

 $x = new SimpleXmlElement($content);

2 个答案:

答案 0 :(得分:1)

echo $x->location->zip;
//"58200"

演示:http://codepad.org/FJDSM5su

答案 1 :(得分:0)

<?php

$content = '<abc>
            <ls:location>
                  <ls:address1>No 16 GLama</ls:address1>
                  <ls:city>Kuala Lumpur</ls:city>
                  <ls:zip>58200</ls:zip>
                  <ls:latitude>3.092055</ls:latitude>
                  <ls:longitude>101.684757</ls:longitude>
            </ls:location>
            </abc>';


$x = new SimpleXmlElement($content);

// accessing the wanted value in the object
// returns zip as an object
$zip = $x->location->zip;

// object to string type conversion
$zip = (string) $zip;

assert('$zip == "58200" /* Expected result: zip = 58200. */');
?>