我尝试从json数组中提取数据但总是得到空字符串。
我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<ItemList>
<Entry Item = "test1" Object = "1tset" />
<Entry Item = "test2" Object = "2tset" />
</ItemList>
我的XML转储:
array(1) { ["Entry"]=> array(2)
{ [0]=> array(1) { ["@attributes"]=> array(2) { ["Item"]=> string(5) "test1" ["Object"]=> string(5) "1tset" } }
[1]=> array(1) { ["@attributes"]=> array(2) { ["Item"]=> string(5) "test2" ["Object"]=> string(5) "2tset" } } } }
我的PHP代码:
$xmlString = "my.xml";
$xmlObject = simplexml_load_file($xmlString);
$xmltovar = json_decode(json_encode($xmlObject), true);
foreach($xmltovar['Entry'] as $test) {
echo $test['Item']."<br>";
}
我需要从ItemList-&gt; Entry Tag获得所有“Item”或“Object”。
答案 0 :(得分:1)
编码和解码后,$xmltovar
就像这样:
Array
(
[Entry] => Array
(
[0] => Array
(
[@attributes] => Array
(
[Item] => test1
[Object] => 1tset
)
)
[1] => Array
(
[@attributes] => Array
(
[Item] => test2
[Object] => 2tset
)
)
)
)
所以而不是:
echo $test['Item']."<br>";
写:
echo $test['@attributes']['Item']."<br>";
对象也是如此。
答案 1 :(得分:0)
你的php必须看起来像这样
<?php
$xmlString = "my.xml";
$xmlObject = simplexml_load_file($xmlString);
$xmltovar = json_decode(json_encode($xmlObject), true);
foreach($xmltovar['Entry'] as $test) {
echo $test["@attributes"]['Item']."<br>";
}
?>
答案 2 :(得分:0)
$ test [&#39; @ attributes&#39;] [&#39; Item&#39;] for Item
$ test [&#39; @ attributes&#39;] [&#39; Object&#39;] for Object