0

我有一个这样的 XML

<items>
  <item>
    <name>A</name>
    <phone>1111111</phone>
  </item>
  <item>
    <name>B</name>
    <phone>2222222</phone>
  </item>
</items>

如何将唯一的 nodeNames 放入数组中,例如 array("name","phone");

4

1 回答 1

2
$xml = '<items>
  <item>
    <name>A</name>
    <phone>1111111</phone>
  </item>
  <item>
    <name>B</name>
    <phone>2222222</phone>
  </item>
</items>';
$obj = new SimpleXMLElement($xml);
$arr = json_decode(json_encode($obj), TRUE);
$arr = $arr['item'];
var_dump($arr);

输出:

 Array
(
    [0] => Array
        (
            [name] => A
            [phone] => 1111111
        )

    [1] => Array
        (
            [name] => B
            [phone] => 2222222
        )
)

如果要获取数组键,可以这样做:

$keys = array_keys($arr[0]);
var_dump($keys);

输出:

Array
(
    [0] => name
    [1] => phone
)
于 2013-07-17T15:45:03.613 回答