多维数组PHP中缺少索引 [英] Missing index in a multidimensional array PHP

查看:76
本文介绍了多维数组PHP中缺少索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我需要找出多维数组中缺少的索引。

As the title suggests I need to find out the missing index in a multidimensional array .

示例:

[1] => Array
    (
        [1] => User Name
        [2] => empID
        [3] => type
        [4] => First Name
        [5] => Last Name
        [6] => email
        [7] => survey_complete
        [8] => login_limit
        [9] => multi_use
    )

[2] => Array
    (
        [1] => fdsfdsf
        [2] => 123
        [3] => 1
        [4] => dsdsa
        [5] => dasdsad
        [6] => j@j.com
        [7] => xzczxcxz
        [8] => czxcxz
        [9] => 1
    )

[3] => Array
    (
        [1] => dsadsada
        [2] => 123
        [3] => 1
        [4] => dasda
        [5] => dsadsadasd
        [6] => j@j.com
        [7] => dsdsada
        [8] => dsadsadsa
        [9] => 1
    )

[4] => Array
    (
        [1] => fdsfdsf
        [2] => 123
        [3] => 1
        [4] => dsadas
        [5] => aaa
        [6] => j@j.com
        [7] => dsdsada
        [8] => dsadsadsa
        [9] => 0
    )

[5] => Array
    (
        [1] => fdsfdsf
        [2] => 123
        [3] => 1
        [4] => dssa
        [5] => cxzcczxczxcxz
    )

[7] => Array
    (
        [1] => MANDATORY FIELD
    )

[8] => Array
    (
        [1] => multi_use: Enter multiuse as 0.
    )

在上面的示例中,缺少索引6。有没有一种方法可以找出php中缺少哪个索引(索引6),也可以计算出PHP中缺少索引(存在2个索引之后的索引)之后存在的索引数目?

Here in the above example index 6 is missing . Is there a way to find out in php which index(index 6) is missing and also a way to calculate the number of indexes present after the missing index in PHP(2 indexes present after 6)?

谢谢,
贾斯汀

Thank you , Justin

推荐答案

请参见下面的示例,了解如何丢失密钥。 / p>

See below example how to get missing key.

    $array = array(
        1 => 123,
        2 => 456,
        4 => 789, 
        8 => 789, 
    );
    $firstkey = key($array); // get first index of array
    end($array);         
    $lastkey = key($array);  // get last index of array

   for($i = $firstkey;$i <= $lastkey;$i++)
    {

        if(!array_key_exists($i,$array)) // check key exist or not
        {
            echo "Missing Array key is ".$i."<br/>";
        }

    }

O / p:-

Missing Array key is 3
Missing Array key is 5
Missing Array key is 6
Missing Array key is 7




注意::-仅用于编号索引。 / p>

NOTE::- Only for numbered index.

这篇关于多维数组PHP中缺少索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆