2

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

Example :

[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.
    )

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

mrun
  • 724
  • 6
  • 18
  • 22
Justin
  • 235
  • 1
  • 3
  • 12
  • There is no ready-to-go solution for this, since you ask for a very specific information. You will have to code your own function based on a simple iteration over the indexes of your array. – arkascha Nov 24 '15 at 08:00
  • It is not clear to me, what exactly you mean by "missing index". Can an index be "missing" somewhere in between? Does the first array define the "standard"? What exactly do you need, just "number N is missing somewhere", or "array K is missing numbers N, M and J"? – cdonat Nov 24 '15 at 08:02
  • @cdonat yes , the array starts from index 1 . As shown in the example every array I generate does have a missing index . I will have to find that and start using it in my code . – Justin Nov 24 '15 at 08:05
  • @Justin sorry, but can you please try and answer my questions? What exactly does "missing index" mean? – cdonat Nov 24 '15 at 08:09

4 Answers4

0

Just use array_keys() and loop through it to find the missing.

array_keys($arr1);
Sanjay Kumar N S
  • 4,165
  • 3
  • 18
  • 34
0

This might not be the best way to do it but you can make an exception there, the idea is to move through the array while saving the position it is in, then if the array is missing an index it's gonna throw and exception and you can echo or just returning the position of the missing array and if there's no missing index return a -1, it's something like this:

<?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'
    }
}
?>

Hope it was useful!

0

The function getMissingArrayKeys() which I've written below can give you the missing index:

<?php

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

Sample Usage:

<?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";
}

As you see above, I have assumed that your array should contain indexes from 1 to 8, generated by range(1, 8), which is used as a reference array.

0

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

NOTE::- Only for numbered index.

Parth Chavda
  • 1,731
  • 1
  • 18
  • 26