缺少多维数组PHP中的索引

时间:2015-11-24 07:56:22

标签: php arrays 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中缺少索引后出现的索引数(6个索引后出现的2个索引)?

谢谢你, 贾斯汀

4 个答案:

答案 0 :(得分:0)

只需使用array_keys()并遍历它就可以找到丢失的内容。

array_keys($arr1);

答案 1 :(得分:0)

这可能不是最好的方法,但你可以在那里做一个例外,想法是在保存它所在的位置的同时移动数组,然后如果数组缺少一个索引它会抛出异常并且你可以echo或只返回缺失数组的位置,如果没有丢失索引则返回-1,它是这样的:

<?php
for ($x = 0; $x <= xLimit; $x++) {
    for ($y = 0; $y <= yLimit; $y++) {
        // Here you place the exception
        try {
            // Just try to enter the index to force and error if it doesn't exist
        } catch (Exception $e) {
            // Here you return or echo the position '(x,y)' you where when the exception ocures
        }
        // Else, just return something that let you know it didn't had any problem ej. '-1'
    }
}
?>

希望它有用!

答案 2 :(得分:0)

我在下面写的函数getMissingArrayKeys()可以为您提供缺失的索引:

<?php

function getMissingArrayKeys($arr, $ref) {
    return array_diff(
        array_values($ref),
        array_keys($arr)
    );
}

样本用法:

<?php

$my_array = array(
    '1' => 'for',
    '2' => 'bar',
    '6' => 'qux',
);

$missing = getMissingArrayKeys($my_array, range(1, 8));
foreach ($missing as $m) {
    echo 'Missing Index: ', $m, "\n";
}

如上所述,我假设您的数组应包含1到8之间的索引,由范围(1,8)生成,用作参考数组。

答案 3 :(得分:0)

请参阅以下示例如何获取缺失密钥。

    $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
  

注意:: - 仅用于带编号的索引。