gpt4 book ai didi

php - 使用 PHP 从 XML 表中提取 NHL 排名

转载 作者:行者123 更新时间:2023-11-29 06:32:16 24 4
gpt4 key购买 nike

我正在从事一个项目,在该项目中我提取有关 NHL 的各种统计数据并将它们插入到 SQL 表中。目前,我正在抓取阶段工作,并找到了一个我已经实现的 XML 解析器,但我终究无法弄清楚如何从中提取信息。该表可在此处找到 -> http://www.tsn.ca/datafiles/XML/NHL/standings.xml .解析器应该生成一个多维数组,我只是想从“信息团队”部分提取所有统计数据,但我不知道如何从数组中提取该信息。我将如何去拉动蒙特利尔的胜利次数? (仅作为其余统计数据的示例)这是页面当前的样子 -> http://mattegener.me/school/standings.php这是代码:

<?php
$strYourXML = "http://www.tsn.ca/datafiles/XML/NHL/standings.xml";
$fh = fopen($strYourXML, 'r');
$dummy = fgets($fh);
$contents = '';
while ($line = fgets($fh)) $contents.=$line;
fclose($fh);
$objXML = new xml2Array();
$arrOutput = $objXML->parse($contents);
print_r($arrOutput[0]); //This print outs the array.

class xml2Array {

var $arrOutput = array();
var $resParser;
var $strXmlData;

function parse($strInputXML) {

$this->resParser = xml_parser_create ();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");

xml_set_character_data_handler($this->resParser, "tagData");

$this->strXmlData = xml_parse($this->resParser,$strInputXML );
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)));
}

xml_parser_free($this->resParser);

return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($this->arrOutput,$tag);
}

function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
}
else {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
}
}
}

function tagClosed($parser, $name) {
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this- >arrOutput)-1];
array_pop($this->arrOutput);
}
}


?>

最佳答案

将这个搜索功能添加到你的类中并使用这段代码

$objXML = new xml2Array();
$arrOutput = $objXML->parse($contents);
// first param is always 0
// second is 'children' unless you need info like last updated date
// third is which statistics category you want for example
// 6 => the array you want that has wins and losses
print_r($arrOutput[0]['children'][6]);
//using the search function if key NAME is Montreal in the whole array
//result will be montreals array
$search_result = $objXML->search($arrOutput, 'NAME', 'Montreal');
//first param is always 0
//second is key name
echo $search_result[0]['WINS'];

function search($array, $key, $value)
{
$results = array();

if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;

foreach ($array as $subarray)
$results = array_merge($results, $this->search($subarray, $key, $value));
}

return $results;
}

小心
此搜索功能区分大小写,需要进行修改,例如匹配到将蒙特利尔的大写字母 M 更改为小写字母的键或值的百分比将为空

关于php - 使用 PHP 从 XML 表中提取 NHL 排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com