dsvyc66464 2013-10-09 13:59
浏览 43
已采纳

二维数组

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

  • 写回答

1条回答 默认 最新

  • donglang7236 2013-10-09 15:14
    关注

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

    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 "
    ";
    

    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 :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100