我有一个这种格式的 xml 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<adp_report name="Average Draft Position - Mixed 5x5" rundate="2013-01-29 17:22:10.0" begin="2013-01-26" end="2013-01-29" draftcount="126">
<player id="500736" name="Mike Trout" position="OF" team="ANA" adp="1.66" early="1" late="4" selections="126" />
<player id="291154" name="Ryan Braun" position="OF" team="MIL" adp="2.01" early="1" late="4" selections="126" />
<player id="213968" name="Miguel Cabrera" position="3B" team="DET" adp="2.55" early="1" late="4" selections="126" />
</adp_report>
我需要将它加载到 php 中,我可以通过查找 name 属性和相应的 adp 来访问它。我正在使用它来执行一些计算并将结果插入到从 MYSQL 数据库调用的表中。这是我到目前为止所拥有的:
$url = '...some url';
$xml = simplexml_load_file($url);
while($row = mysqli_fetch_array($resultbat, MYSQLI_ASSOC))
{
echo "<td>" . $row['NAME'] . "</td>";
...print out some more player data from database...
foreach($xml->player as $player)
{
$attr = $player->attributes();
if($attr['name'] == $row['NAME']) //$row['NAME'] is the players name from my database
{
$adp = (float) $attr['adp'];
$early = (int) $attr['early'];
$stdev = -0.42+0.42*($adp-$early);
if($stdev<0)
$stdev = 1;
$chance =number_format(((1-NORMDIST($pickNumber,$adp,$stdev,TRUE))*100), 0);
echo "<td class='adp'>".$adp."</td>";
echo "<td class='chance'>".$chance."%</td>";
break;
}
}
}
这需要一些时间来处理,因为我正在浏览我的玩家数据库中的每一行,然后foreach
用于查看 xml 文件,如果找到匹配项,我会进行计算。我不得不想象有一种更有效的方法来解决这个问题。提前致谢。