首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较当前迭代中的数组大小与前一次迭代中的数组大小

如何比较当前迭代中的数组大小与前一次迭代中的数组大小
EN

Stack Overflow用户
提问于 2015-06-04 04:52:27
回答 1查看 42关注 0票数 0

$array_size在每次迭代中都给出数组$expkey的数组大小。在每次迭代中,我都希望比较当前数组大小和前一个数组大小。如何做到这一点,加上在第一次迭代中,没有prev数组大小,所以它不应该提供一些偏移索引。那该怎么做呢。谢谢你提前帮忙。

代码语言:javascript
复制
<?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);

}
?>  

关于打印$array_size输出是

代码语言:javascript
复制
1
1
5
5
8
8
11
11
8
8
5
5
5
5
5
5
1
1
5
5
5
5

$array

代码语言:javascript
复制
 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
)

当我检查$expkey的数组大小时,

代码语言:javascript
复制
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
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-04 05:27:51

在计算final ($array_size)之后,可以尝试并存储一个前后值:

代码语言:javascript
复制
<?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;
        }
?>

给你:

代码语言:javascript
复制
1 is first
3 is more than 1
4 is more than 3
1 is less than 4
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30635178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档