简体   繁体   English

在php数组中存储xml属性

[英]storing xml attributes in php array

I have a xml file in this format: 我有以下格式的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>

I need to load this into php where I will be able to access it by finding the name attribute and what the corresponding adp is. 我需要将其加载到php中,通过找到name属性以及相应的adp是可以访问它。 I am using it to perform some calculations and insert the results into a table that is being called from a MYSQL database. 我正在使用它执行一些计算,并将结果插入到从MYSQL数据库调用的表中。 This is what I have so far: 这是我到目前为止的内容:

$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;
                }
            }
}

This takes a while to process because I'm going through every row in my player database, then using foreach to look through the xml file and if I find a match I do the calculations. 这需要花一些时间,因为我要遍历播放器数据库中的每一行,然后使用foreach浏览xml文件,如果找到匹配项,我就会进行计算。 I have to imagine there is a more efficient way to go about this. 我必须想象有一种更有效的方法可以解决此问题。 Thanks in advance. 提前致谢。

You could use XPath (see: SimpleXMLElement::xpath() and XPath documentation ). 您可以使用XPath(请参阅: SimpleXMLElement :: xpath()XPath文档 )。 So, instead of your foreach loop, this: 因此,这不是您的foreach循环:

$player = $xml->xpath('//player[@name="' . $row['NAME'] . '"]');
if (is_array($player) && count($player)) {
    $player = $player[0];
} else {
    continue; // not found
}
$attr = $player->attributes();
// and so on...

I'd pre-process the XML file to an associative-array (user-id/user-name => properties); 我将XML文件预处理为一个关联数组(用户ID /用户名=>属性);

$url = '...some url';
$xml = simplexml_load_file($url);

$players = array();

foreach($xml->player as $player) {
    $attr = $player->attributes();

    // cast the SimpleXMLElement to a string
    $username = (string) $attr['name'];
    $players[$username] = $attr;
}

Then retrieve the data from the database and match the results from XML 然后从数据库检索数据并匹配XML的结果

while($row = mysqli_fetch_array($resultbat, MYSQLI_ASSOC)) {
     if(!array_key_exists($row['NAME'], $players)) {
          // player was not found in the XML
          continue;
     }

     // get the player's properties by matching the 'name'
     $player_stats = $players[$row['NAME']];

     // here, process player_stats

}

However, your code assumes the player-name to be unique, it may be possible that multiple players share the same name (this problem was already present in your current code) 但是,您的代码假定玩家名称是唯一的,所以可能有多个玩家共享同一名称(当前代码中已经存在此问题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM