[英]Extract Data from a JSON array with php
我尝试从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-> Entry Tag中获取所有“ Item”或“ Object”。
编码和解码$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>";
对象也一样。
您的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>";
}
?>
项目的$ test ['@ attributes'] ['Item']
$ test ['@ attributes'] ['Object']代表对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.