user1089366
user1089366

Reputation: 57

Two dimensional array

i've tried to find this out by myself before asking but cant really figure it out.

What I have is a loop, it's actually a loop which reads XML data with simplexml_load_file

Now this XML file has data which I want to read and put into an array.. a two dimensional array actually..

So the XML file has a child called Tag and has a child called Amount. The amount is always differnt, but the Tag is usually the same, but can change sometimes too.

What I am trying to do now is:

Example:

This is the XML example:

<?xml version="1.0"?>
<Data>
<Items>
    <Item Amount="9,21" Tag="tag1"/>
    <Item Amount="4,21" Tag="tag1"/>
    <Item Amount="6,21" Tag="tag2"/>
    <Item Amount="1,21" Tag="tag1"/>
    <Item Amount="6,21" Tag="tag2"/>

</Data>
</Items>

Now i have a loop which reads this, sees what tag it is and adds up the amounts. It works with 2 loops and two different array, and I would like to have it all in one array in single loop.

I tried something like this:

$tags = array();
        for($k = 0; $k < sizeof($tags); $k++)
        {
                if (strcmp($tags[$k], $child['Tag']) == 0)
            {
                $foundTAG = true;
                break;
            }
            else
                $foundTAG = false;
        }


        if (!$foundTAG)
        {
            $tags[] = $child['Tag'];
        }

and then somewhere in the code i tried different variations of adding to the array ($counter is what counts the Amounts together):

$tags[$child['Tag']][$k] = $counter;
$tags[$child['Tag']][] = $counter;
$tags[][] = $counter;

i tried few other combinations which i already deleted since it didnt work..

Ok this might be a really noob question, but i started with PHP yesterday and have no idea how multidimensional arrays work :)

Thank you

Upvotes: 0

Views: 93

Answers (1)

Cwissy
Cwissy

Reputation: 2156

this is how you can iterate over the returned object from simple xml:

$xml=simplexml_load_file("/home/chris/tmp/data.xml");
foreach($xml->Items->Item as $obj){
    foreach($obj->Attributes() as $key=>$val){
        // php will automatically cast each of these to a string for the echo
        echo "$key = $val\n";
    }
}

so, to build an array with totals for each tag:

$xml=simplexml_load_file("/home/chris/tmp/data.xml");
$tagarray=array();
// iterate over the xml object
foreach($xml->Items->Item as $obj){
    // reset the attr vars.
    $tag="";
    $amount=0;
    // iterate over the attributes setting
    // the correct vars as you go
    foreach($obj->Attributes() as $key=>$val){
        if($key=="Tag"){
            // if you don't cast this to a
            // string php (helpfully) gives you
            // a psuedo simplexml_element object
            $tag=(string)$val[0];
        }
        if($key=="Amount"){
            // same as for the string above
            // but cast to a float
            $amount=(float)$val[0];
        }
        // when we have both the tag and the amount
        // we can store them in the array
        if(strlen($tag) && $amount>0){
            $tagarray[$tag]+=$amount;
        }
    }
}
print_r($tagarray);
print "\n";

This will break horribly should the schema change or you decide to wear blue socks (xml is extremely colour sensitive). As you can see dealing with the problem child that is xml is tedious - yet another design decision taken in a committee room :-)

Upvotes: 1

Related Questions