Charlie Harding
Charlie Harding

Reputation: 685

PHP empty key in array

In PHP, is there a function for finding the lowest numerical key without a value in an array? I have an array like this:

array (
    0 => 'nested array',
    1 => 'nested array',
    2 => 'nested array',
    3 => 'nested array',
    6 => 'nested array',
    7 => 'nested array',
)

and I would like to add a value to this array (at 4), not necessarily unique, and get its position back without altering the rest of the keys. Is the easiest way to loop through the array and test isset(), or is there an easier way?

Upvotes: 2

Views: 2278

Answers (5)

salathe
salathe

Reputation: 51950

In PHP, is there a function for finding the lowest numerical key without a value in an array?

No.

Is the easiest way to loop through the array and test isset()

Looping is okay, but isset() isn't going to be the best choice. It could raise a false-positive where an array key exists but contains a NULL value. A better choice would be array_key_exists().

In your case, a loop can be as simple as:

for ($key = 0; array_key_exists($key, $arr); $key++);
var_dump($key);

or is there an easier way?

See the other answers suggesting alternatives, which may or may not be easier.

Upvotes: 3

Darragh Enright
Darragh Enright

Reputation: 14136

You could try the following to search for the lowest key:

$arr = array (
    0 => 'nested array',
    1 => 'nested array',
    2 => 'nested array',
    3 => 'nested array',
    6 => 'nested array',
    7 => 'nested array',
);

// get a list of existing keys
$keys = array_keys($arr);

// get the min and max keys
$min = min($keys);
$max = max($keys);

// get the missing keys by:
// * creating a range from the existing min/max
// *diff it from the list of actual keys
$missing = array_diff(range($min, $max), $keys);

// get the min missing key from the diff
echo min($missing);

Hope this helps :)

Upvotes: 4

Anthony
Anthony

Reputation: 37065

You can get an array of all open keys by using range() and array_diff along with array_keys:

$array = array (
    0 => 'nested array',
    1 => 'nested array',
    2 => 'nested array',
    3 => 'nested array',
    6 => 'nested array',
    7 => 'nested array',
);

$array_keys = array_keys($array);
sort($array_keys);

$min_key = 0;
$max_key = end($array_keys);

$array_key_range = range($min_key, $max_key);

$open_keys = array_diff($array_key_range , $array_keys);

You can then use the first value in that array as the lowest, or any, if you just want to find an open slot.

Upvotes: 0

Rudie
Rudie

Reputation: 53831

You could do something like this. I think it looks cool, but I wouldn't do it, because it's not very efficient:

$array = array(0 => 'x', 1 => 'x', 2 => 'x', 4 => 'x', 7 => 'x');

$indexes = array_keys($array); // 0, 1, 2, 4, 7
// sort($array, SORT_NUMERIC);
$all_indexes = range($indexes[0], $indexes[count($indexes)-1]); // 0, 1, 2, 3, 4, 5, 6, 7
$missing_indexes = array_diff($all_indexes, $indexes); // 3, 5, 6
print_r($missing_indexes);

$lowest = reset($missing_indexes);
$highest = end($missing_indexes);
var_dump($lowest, $highest);

You can compact it in many less lines if that's your thing.

You now have all the missing index information: how many, which, lowest, highest, average etc

Upvotes: 0

Kamehameha
Kamehameha

Reputation: 5473

This works -

function find_empty($arr){
    foreach(range(0,max(array_keys($arr))) as $i){
        if(!array_key_exists($i, $arr)){
            return $i;
        }
    }
    return -1;
}

Testing it -

$arr = array (
    0 => 'nested array',
    1 => 'nested array',
    2 => 'nested array',
    3 => 'nested array',
    6 => 'nested array',
    7 => 'nested array',
);
var_dump(find_empty($arr));
/**
OUTPUT
int 4
**/

Upvotes: 0

Related Questions