Jump to content

PHP + Big Array


tready29483

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.