douzhangli9563 2017-04-04 14:11
浏览 119

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

I have all of the JSON data in @attributes and want to extract it to variables using PHP.
What i am trying:

$url = "http://synd.cricbuzz.com/j2me/1.0/livematches.xml";
$xml = simplexml_load_file($url) or die("Error..");
$json = json_encode($xml, JSON_PRETTY_PRINT);
$decode_json = json_decode($json);
$match = $decode_json->match[0];
print_r($match);

Here is the output i get using above code: http://phpfiddle.org/main/code/3zbq-62w0
Any help will be appreciated. Thanks..

  • 写回答

2条回答 默认 最新

  • douren0558 2017-04-04 14:23
    关注

    @Sukhchain Singh simply try below one:

    <?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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用