doushouxie7064 2015-06-04 04:52
浏览 65
已采纳

如何将当前迭代中的数组大小与先前迭代中的数组大小进行比较

$array_size is giving array size of array $expkey in each iteration. On each iteration I want to compare present array size with the previous array size. How to do this plus in on first iteration there is no prev array size so it should not give some offset index. So how to do it. Thank you for help in advance.

<?php

if (isset($_POST['submit'])) {

    unset($_POST['submit']);
    unset($_POST['ew-language__en']);

    function printxml ($array)
    {
        $xml = new DOMDocument('1.0','UTF-8');
        $xpath = new DOMXPath($xml);
        $xml->formatOutput = true;

        $last_index_ids = array();
        $parentid = array();
        $i_id=0;
        $x=0;

        foreach ($array as $key => $value)
        {

            $key = $key;
            //contains all the values of respective ids
            $attrvalue= $value;

            if ($array[$key] == "" || ctype_space($array[$key]) ) {
                $array[$key] = null;
            }
            //split unique input field names
            $expkey = explode("__", $key);

            $array_size=sizeof($expkey);

            echo"<pre>"; print_r($array_size); echo "</pre>";

            //first tag <ew-lang>
            $first_key =($expkey[0]);


            print_r($array_size);



        }
        $xml->save("file.xml");
    }
    printxml($_POST);

}
?>  

on printing $array_size output is

1
1
5
5
8
8
11
11
8
8
5
5
5
5
5
5
1
1
5
5
5
5

$array is

 Array
(
    [global] => global
    [ew-language__en__0__phrase__actiondeleted] => Deleted
    [ew-language__en__0__phrase__actiondeleted__0__child_phrase__1234] => numbers
    [ew-language__en__0__phrase__actiondeleted__0__child_phrase__1234__0__child_phrase_1__abc] => test
    [ew-language__en__0__phrase__actiondeleted__1__child_phrase_2__5678] => numerics
    [ew-language__en__1__phrase__actioninserted] => Inserted
    [ew-language__en__2__phrase__actioninsertedgridadd] => Inserted
    [ew-language__en__3__phrase__actionupdated] => Updated
    [project] => project
    [ew-language__en__0__phrase__actioninserted] => Inserted
    [ew-language__en__1__phrase__actioninsertedgridadd] => Inserted
)

where as I am checking array size of $expkey which is

Array
(
    [0] => global
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 0
    [3] => phrase
    [4] => actiondeleted
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 0
    [3] => phrase
    [4] => actiondeleted
    [5] => 0
    [6] => child_phrase
    [7] => 1234
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 0
    [3] => phrase
    [4] => actiondeleted
    [5] => 0
    [6] => child_phrase
    [7] => 1234
    [8] => 0
    [9] => child_phrase_1
    [10] => abc
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 0
    [3] => phrase
    [4] => actiondeleted
    [5] => 1
    [6] => child_phrase_2
    [7] => 5678
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 1
    [3] => phrase
    [4] => actioninserted
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 2
    [3] => phrase
    [4] => actioninsertedgridadd
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 3
    [3] => phrase
    [4] => actionupdated
)
Array
(
    [0] => project
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 0
    [3] => phrase
    [4] => actioninserted
)
Array
(
    [0] => ew-language
    [1] => en
    [2] => 1
    [3] => phrase
    [4] => actioninsertedgridadd
)
  • 写回答

1条回答 默认 最新

  • duanjue6575 2015-06-04 05:27
    关注

    Maybe try and store a before and after value after calculating final ($array_size):

    <?php
    
        $arr[]  =   1;
        $arr[]  =   3;
        $arr[]  =   4;
        $arr[]  =   1;
    
        // This is just a hypothetical loop just to demonstrate concept
        // representing the loop: foreach ($array as $key => $value)
        foreach($arr as $array_size) {
    
                //******************************************************//
                // Do all your code that gets to to the final count here
                //******************************************************//
    
                $calculated =   $array_size;
    
                if(isset($check)) {
                        if($calculated < $check)
                            echo $calculated.' is less than '.$check;
                        else
                            echo $calculated.' is more than '.$check;
    
                        echo '<br />';
                    }
                else
                    echo $calculated.' is first'."<br />";
    
                $check      =   $array_size;
            }
    ?>
    

    Gives you:

    1 is first
    3 is more than 1
    4 is more than 3
    1 is less than 4
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址