【问题标题】:simple xml changes array key name简单的 xml 更改数组键名
【发布时间】:2023-04-08 14:43:03
【问题描述】:

所以我通过 api 请求和响应得到 xml 数据,看起来像这样

 <round round_id="21402" title="Final" start_date="2016-03-06"
     end_date="2016-03-06" scoringsystem="BO3" groups="0" ties="0"
     ordermethod="1" prize_money="6400" prize_currency="usd"
     last_updated="2016-03-06 12:32:41">

     <match match_id="154374" official_start_date="2016-03-06" official_start_time="08:00:00" actual_start_date="" actual_start_time="" winner="B" score_A="1" score_B="2" status="Played" drawposition="1" person_A_id="" person_A_name="" person_A_country="" person_B_id="" person_B_name="" person_B_country="" double_A_id="15014" double_A_country="" person_A1_id="27475" person_A1_name="Liang Chen" person_A1_country="CHN" person_A2_id="29558" person_A2_name="Wang Yafan" person_A2_country="CHN" double_B_id="17263" double_B_country="" person_B1_id="27351" person_B1_name="V. Wongteanchai" person_B1_country="THA" person_B2_id="30339" person_B2_name="Yang Zhaoxuan" person_B2_country="CHN" last_updated="2016-03-06 12:32:41" time_unknown="no">
       <matchstatistics>
         <double double_id="15014" name="Liang Chen / Wang Yafan">
           <statistics aces="1" dbl_faults="3" first_srv_point_won="31"   first_srv_point_total="46" sec_srv_point_won="7" sec_srv="21" break_point_won="6" break_point_total="8" service_games_played="10" first_ret_point_won="13" first_ret_point_total="42" sec_ret_point_won="11" sec_ret_point="19" break_ret_point_won="2" break_ret_total="6" return_games_played="10" total_service_points_won="38" total_service_points_total="67" total_return_points_won="24" total_return_points_total="61" total_points_won="62" total_points="128"/>
         </double>
         <double double_id="17263" name="V. Wongteanchai / Yang Zhaoxuan">
            <statistics aces="1" dbl_faults="2" first_srv_point_won="29" first_srv_point_total="42" sec_srv_point_won="8" sec_srv="19" break_point_won="4" break_point_total="6" service_games_played="10" first_ret_point_won="15" first_ret_point_total="46" sec_ret_point_won="14" sec_ret_point="21" break_ret_point_won="2" break_ret_total="8" return_games_played="10" total_service_points_won="37" total_service_points_total="61" total_return_points_won="29" total_return_points_total="67" total_points_won="66" total_points="128"/>
         </double>
       </matchstatistics>
       <set set_id="352608" score_A="6" score_B="4" tiebreakscore_A="" tiebreakscore_B="" start_date="" start_time="" end_date="" end_time="" last_updated="2016-03-06 12:32:41"/>
       <set set_id="352609" score_A="4" score_B="6" tiebreakscore_A="" tiebreakscore_B="" start_date="" start_time="" end_date="" end_time="" last_updated="2016-03-06 12:32:41"/>
       <set set_id="352610" score_A="7" score_B="10" tiebreakscore_A="" tiebreakscore_B="" start_date="" start_time="" end_date="" end_time="" last_updated="2016-03-06 12:32:41"/>
    </match> 
</round>

我用 simplexml_load_string() 加载 xml 数据,这个逻辑来处理数据

foreach ($matches->xpath('//round') as $roundData) {

       $round = $this->roundsEtl->processRound($roundData, $season);

           foreach ($roundData->match as $matchData) {
                  d($matchData);
                  $game = $this->processGame($season, $round, $matchData);

                   $output->writeln("Game: ".$game->getSlug());

           }
 }

但是,转储 $matchData 会给出除白发之外的数据数组,其中统计节点名称(数组键)更改为 0。

public matchstatistics -> SimpleXMLElement (1) (
        public double -> array (2) [
            SimpleXMLElement (2) (
                public @attributes -> array (2) [
                    'double_id' => string (5) "26961"
                    'name' => string (7) "J. Zopp/K. Yoo"
                ]
                public 0 -> SimpleXMLElement (1) (
                    public @attributes -> array (22) [
                        'aces' => string (1) "2"
                        'dbl_faults' => string (1) "1"
                        'first_srv_point_won' => string (2) "17"
                        'first_srv_point_total' => string (2) "32"
                        'sec_srv_point_won' => string (1) "7"

                    ]
                )
            )
            SimpleXMLElement (2) (
                public @attributes -> array (2) [
                    'double_id' => string (5) "11600"
                    'name' => string (8) "T. Kamke/J. Grobe"
                ]
                public statistics -> SimpleXMLElement (1) (
                    public @attributes -> array (22) [
                        'aces' => string (1) "2"
                        'dbl_faults' => string (1) "1"
                        'first_srv_point_won' => string (2) "22"
                        'first_srv_point_total' => string (2) "35"
                        'sec_srv_point_won' => string (2) "13"
                        'sec_srv' => string (2) "19"

                    ]
                )
            )
        ]
    )

感谢任何帮助

【问题讨论】:

  • 与此问题无关。我想访问 xml/SimpleXML 对象中的统计节点,但我不能,因为它的值已更改为 0,如上所述并记录在上面
  • SimpleXML 不创建数组。如果您记得SimpleXML 是神奇的,并且您不能相信转储函数,您的头发就会少很多。坚持您要访问的内容,然后从那里开始。

标签: php xml simplexml


【解决方案1】:

您过于依赖转储数据,而过于依赖逻辑上的结构并尝试访问内容。 SimpleXML 使用了很多魔法让事情在您要求时就可以正常工作,并且不会“包含”特定格式的数据。

你要找的任务就是这么简单(live demo):

foreach ($matches->xpath('//round') as $roundData) {
    foreach ($roundData->match as $matchData) {
        foreach ( $matchData->matchstatistics->double as $double ) {
            echo (string) $double->statistics['first_srv_point_won'], " First Serve Points Won\n";
        }
    }
}

【讨论】:

  • 虽然我在转储你的方法时得到了相同的结果 :) 必须是某种 SimpleXML 魔法,如前所述。谢谢
  • @3ND 是的,更具体一点,SimpleXML 重载语法,如数组访问([0]['foo'])、属性访问(-&gt;foo)和转换为字符串(@987654327 @, echo $foo) 让您的生活更轻松。例如,$foo-&gt;child-&gt;grandChild$foo-&gt;child[0]-&gt;grandChild 相同是非常方便的,但这在var_dump 输出中很难表达。另见github.com/IMSoP/simplexml_debug
猜你喜欢
  • 2023-03-26
  • 2014-05-22
  • 1970-01-01
  • 2017-05-14
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
相关资源
最近更新 更多