【问题标题】:Missing index in a multidimensional array PHP多维数组PHP中缺少索引
【发布时间】:2015-11-24 07:56:22
【问题描述】:

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

例子:

[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 个索引)?

谢谢你, 贾斯汀

【问题讨论】:

  • 没有现成的解决方案,因为您要求提供非常具体的信息。您必须根据对数组索引的简单迭代来编写自己的函数。
  • 我不清楚,您所说的“缺失索引”到底是什么意思。索引可以在两者之间的某个地方“丢失”吗?第一个数组是否定义了“标准”?您究竟需要什么,只是“数字 N 缺少某处”,或者“数组 K 缺少数字 N、M 和 J”?
  • @cdonat yes ,数组从索引 1 开始。如示例所示,我生成的每个数组都缺少索引。我必须找到它并开始在我的代码中使用它。
  • @Justin 对不起,你能试着回答我的问题吗? “缺失索引”到底是什么意思?

标签: php arrays multidimensional-array


【解决方案1】:

请参阅下面的示例如何获取丢失的密钥。

    $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

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

【讨论】:

    【解决方案2】:

    只需使用 array_keys() 并循环查找丢失的部分。

    array_keys($arr1);
    

    【讨论】:

      【解决方案3】:

      这可能不是最好的方法,但你可以在那里做一个异常,想法是在保存它所在位置的同时在数组中移动,然后如果数组缺少索引,它会抛出异常你可以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'
          }
      }
      ?>
      

      希望有用!

      【讨论】:

        【解决方案4】:

        我在下面编写的函数 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 的索引,由 range(1, 8) 生成,用作参考数组。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-04
          • 1970-01-01
          相关资源
          最近更新 更多