如何使用PHP从json / xml中的@attributes获取数据

时间:2017-04-04 14:11:40

标签: php json xml

我拥有@attributes中的所有JSON数据,并希望使用PHP将其提取到变量。
我在尝试什么:

var $ = require("jquery");

以下是使用上述代码的输出:http://phpfiddle.org/main/code/3zbq-62w0
 任何帮助将不胜感激。感谢..

2 个答案:

答案 0 :(得分:1)

您可以使用scrapy crawl craigs -o items.csv库附带的attributes功能。它返回一个simple_xml,它实现了SimpleXMLElement接口,这意味着你可以遍历:

Traversable

现在很容易获得数据。

<?php
$url = "http://synd.cricbuzz.com/j2me/1.0/livematches.xml";
$xml = simplexml_load_file($url) or die("Error..");
$match_attributes = $xml->match->attributes();

foreach ($match_attributes as $k => $v) {
    printf('%s => %s<br />', $k, $v);
}
?>

答案 1 :(得分:-1)

@Sukhchain Singh只需尝试以下一个:

<?php
    $yourArray = json_encode($yourJson, true);
    /* suppose you got $yourArray = array(
                            "@attributes" => array(
                            "id" => 4,
                            "type" => "T20",
                            "srs" => "Bangladesh tour of Sri Lanka, 2017",
                            "mchDesc" => "SL vs BAN",
                            "mnum" => "1st T20I",
                            "vcity" => "Colombo",
                            "vcountry" => "Sri Lanka",
                            "grnd" => "R.Premadasa Stadium",
                            "inngCnt" => 1,
                            "datapath" => "http://synd.cricbuzz.com/j2me/1.0/match/2017/2017_SL_BAN/SL_BAN_APR04/"
                        )
                    );
         */
extract($yourArray["@attributes"]); // it will extract all the element as a variable
echo $id;