tready29483 Posted November 10, 2007 Share Posted November 10, 2007 Hey, I have a xml file that I can convert into an array for use in my website. My problem is I only need one chunk of this data. The array can be seen here: http://www.godlikegaming.net/xml2.php That xml2.php holds the class that turns my xml into an array for me. The part I need to access is this: [29] => Array ( [name] => TOTAL [attrs] => Array ( [KILLS_PER_MINUTE] => 0.56112336339019 [XP_PER_HOUR] => 210.607211052958 [XP] => 2095.60025198 [KILLS] => 335 [ACCURACY] => 24.7323252962812 [TIME_PLAYED] => 35821 [DEATHS] => 496 [KILL_DEATH_RATIO] => 0.675403225806452 ) ) Can anyone help me with this? I'd like to turn all those values into variables that I can use in my code. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/ Share on other sites More sharing options...
rajivgonsalves Posted November 10, 2007 Share Posted November 10, 2007 Is the position of the array always going to be at the 29 index or it might change ? Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388457 Share on other sites More sharing options...
tready29483 Posted November 10, 2007 Author Share Posted November 10, 2007 Its going to stay there as far as I know. I have yet to see it change Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388458 Share on other sites More sharing options...
tready29483 Posted November 10, 2007 Author Share Posted November 10, 2007 I was wrong, I checked another profile and it moved 1 place. Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388459 Share on other sites More sharing options...
rajivgonsalves Posted November 10, 2007 Share Posted November 10, 2007 try this function fnGetData($arrData) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']=='TOTAL') { return $arrValues['attrs']; } } } $arrTotal = fnGetData($XmlArray); where $XmlArray is your big array I am checking if php has a faster way to do it Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388462 Share on other sites More sharing options...
tready29483 Posted November 10, 2007 Author Share Posted November 10, 2007 Thanks for your help. Not only was it fast but it was just what I needed. Thanks! Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388465 Share on other sites More sharing options...
tready29483 Posted November 10, 2007 Author Share Posted November 10, 2007 I have one more question about this. I also need to get the data from this part of the array. [24] => Array ( [name] => XP [child] => Array ( [0] => Array ( [name] => ENGINEER [attrs] => Array ( [XP] => 998.335088002 [LEVEL_1] => 17 [LEVEL_2] => 11 [LEVEL_3] => 4 [LEVEL_4] => 0 ) ) [1] => Array ( [name] => FIELDOPS [attrs] => Array ( [XP] => 0.0 [LEVEL_1] => 0 [LEVEL_2] => 0 [LEVEL_3] => 0 [LEVEL_4] => 0 ) ) [2] => Array ( [name] => MEDIC [attrs] => Array ( [XP] => 4.0 [LEVEL_1] => 0 [LEVEL_2] => 0 [LEVEL_3] => 0 [LEVEL_4] => 0 ) ) [3] => Array ( [name] => SOLDIER [attrs] => Array ( [XP] => 43.6412493289 [LEVEL_1] => 2 [LEVEL_2] => 0 [LEVEL_3] => 0 [LEVEL_4] => 0 ) ) [4] => Array ( [name] => COVERTOPS [attrs] => Array ( [XP] => 130.829147577 [LEVEL_1] => 1 [LEVEL_2] => 1 [LEVEL_3] => 1 [LEVEL_4] => 0 ) ) ) ) Can you help me with this as well? Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388652 Share on other sites More sharing options...
rajivgonsalves Posted November 10, 2007 Share Posted November 10, 2007 function fnGetData($arrData,$strSelect) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']==$strSelect) { return $arrValues['attrs']; } } } $arrTotal = fnGetData($XmlArray,'ENGINEER'); this will give you the data of ENGINEER Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388665 Share on other sites More sharing options...
tready29483 Posted November 10, 2007 Author Share Posted November 10, 2007 Very nice. You are a great programmer. Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388669 Share on other sites More sharing options...
tready29483 Posted November 10, 2007 Author Share Posted November 10, 2007 Wait, that did work for some reason. When I do a var_dump it comes back NULL. $arrEngXP = fnGetExperience($p->output,'ENGINEER'); $arrFOXP = fnGetExperience($p->output,'FIELDOPS'); $arrMedXP = fnGetExperience($p->output,'MEDIC'); $arrSolXP = fnGetExperience($p->output,'SOLDIER'); $arrCOXP = fnGetExperience($p->output,'COVERTOPS'); var_dump($arrEngXP); function fnGetExperience($arrData,$strType) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']== $strType) { return $arrValues['attrs']; } } } Not sure why. I think Engineer is a child node of a child node. How can I get the value of a child nodes child? Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388680 Share on other sites More sharing options...
rajivgonsalves Posted November 10, 2007 Share Posted November 10, 2007 try this new function function fnGetExperience($arrData,#strArrayKeyReturn,$strType) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']== $strType) { return $arrValues[#strArrayKeyReturn]; } } } $arrCOXP = fnGetExperience(fnGetExperience($p->output,'child','XP'),'attr','COVERTOPS'); Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-388686 Share on other sites More sharing options...
tready29483 Posted November 11, 2007 Author Share Posted November 11, 2007 Hey, That actually returned an error: Warning: Invalid argument supplied for foreach() in /home/content/t/o/m/tommyready/html/xml2.php on line 50 NULL function fnGetExperience($arrData,$strArrayKeyReturn,$strType) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) <--- Line 50 { if ($arrValues['name']== $strType) { return $arrValues[$strArrayKeyReturn]; } } } Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-389179 Share on other sites More sharing options...
rajivgonsalves Posted November 12, 2007 Share Posted November 12, 2007 Yes a little error from my side sorry here is the code function fnGetExperience($arrData,$strArrayKeyReturn,$strType) { foreach ($arrData[0][$strArrayKeyReturn] as $strKey =>$arrValues) <--- Line 50 { if ($arrValues['name']== $strType) { return $arrValues[$strArrayKeyReturn]; } } } Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-389584 Share on other sites More sharing options...
tready29483 Posted November 12, 2007 Author Share Posted November 12, 2007 Sorry bro, same error. I think the problem is here: $arrCOXP = fnGetExperience(fnGetExperience($p->output,'child','XP'),'attr','COVERTOPS'); But i'm not 100% sure. Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-389598 Share on other sites More sharing options...
rajivgonsalves Posted November 12, 2007 Share Posted November 12, 2007 here you go function fnGetExperience($arrData,$strArrayKeyReturn,$strType) { foreach ($arrData[$strArrayKeyReturn] as $strKey =>$arrValues) <--- Line 50 { if ($arrValues['name']== $strType) { return $arrValues[$strArrayKeyReturn]; } } } this should work calling should be fnGetExperience(fnGetExperience($p->output[0],'child','XP'),'attr','COVERTOPS'); Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-389709 Share on other sites More sharing options...
tready29483 Posted November 13, 2007 Author Share Posted November 13, 2007 No, that still didn't work. I tried alot of different ways of doing this and can't figure it out. If I give you the full files do you think that might help figure out whats going on? Here is the page the parses the XML and Reads the array to give me the stats I want <?php $p =& new xmlParser(); $xml = "http://stats.enemyterritory.com/profile/SmoothKilla?xml=true"; if (!fopen($xml,"r")) { die("Cannot open XML data file: $xml"); }else { $p->parse($xml); $arrTotal = fnGetTotals($p->output); $arrUserInfo = fnGetUserInfo($p->output); $arrCOXP = fnGetExperience(fnGetExperience($p->output[0],'child','XP'),'attr','COVERTOPS'); //echo "<pre>"; //print_r($p->output); //echo "</pre><br /><br />"; //var_dump($arrExperience); var_dump($arrCOXP); echo '<br />'. $arrCOXP['XP'] . '<br />'; } function fnGetTotals($arrData) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']=='TOTAL') { return $arrValues['attrs']; } } } function fnGetUserInfo($arrData) { foreach ($arrData[0]['child'] as $strKey =>$arrValues) { if ($arrValues['name']=='USER_INFO') { return $arrValues['attrs']; } } } function fnGetExperience($arrData,$strArrayKeyReturn,$strType) { foreach ($arrData[$strArrayKeyReturn] as $strKey =>$arrValues) { if ($arrValues['name']== $strType) { return $arrValues[$strArrayKeyReturn]; } } } class xmlParser{ var $xml_obj = null; var $output = array(); var $attrs; function xmlParser(){ $this->xml_obj = xml_parser_create(); xml_set_object($this->xml_obj,$this); xml_set_character_data_handler($this->xml_obj, 'dataHandler'); xml_set_element_handler($this->xml_obj, "startHandler", "endHandler"); } function parse($path){ if (!($fp = fopen($path, "r"))) { die("Cannot open XML data file: $path"); return false; } while ($data = fread($fp, 4096)) { if (!xml_parse($this->xml_obj, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_obj)), xml_get_current_line_number($this->xml_obj))); xml_parser_free($this->xml_obj); } } return true; } function startHandler($parser, $name, $attribs){ $_content = array(); $_content['name'] = $name; if(!empty($attribs)) $_content['attrs'] = $attribs; array_push($this->output, $_content); } function dataHandler($parser, $data){ if(!empty($data) && $data!="\n") { $_output_idx = count($this->output) - 1; $this->output[$_output_idx]['content'] .= $data; } } function endHandler($parser, $name){ if(count($this->output) > 1) { $_data = array_pop($this->output); $_output_idx = count($this->output) - 1; $add = array(); if(!$this->output[$_output_idx]['child']) $this->output[$_output_idx]['child'] = array(); array_push($this->output[$_output_idx]['child'], $_data); } } } ?> That is the whole thing. Link to comment https://forums.phpfreaks.com/topic/76731-php-big-array/#findComment-390389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.