Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组中的PHP空键_Php_Arrays - Fatal编程技术网

数组中的PHP空键

数组中的PHP空键,php,arrays,Php,Arrays,在PHP中,是否有用于查找数组中没有值的最低数字键的函数?我有这样一个数组: array ( 0 => 'nested array', 1 => 'nested array', 2 => 'nested array', 3 => 'nested array', 6 => 'nested array', 7 => 'nested array', ) 我想给这个数组添加一个值(在4),不一定是唯一的,并在不改变其余

在PHP中,是否有用于查找数组中没有值的最低数字键的函数?我有这样一个数组:

array (
    0 => 'nested array',
    1 => 'nested array',
    2 => 'nested array',
    3 => 'nested array',
    6 => 'nested array',
    7 => 'nested array',
)
我想给这个数组添加一个值(在
4
),不一定是唯一的,并在不改变其余键的情况下返回其位置。是通过数组循环并测试isset()的最简单方法,还是有更简单的方法?

此方法有效-

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

$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
**/

你可以这样做。我认为它看起来很酷,但我不会这么做,因为它效率不高:

$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);
如果你喜欢的话,你可以把它压缩成更少的行


您现在有了所有缺少的索引信息:多少、哪个、最低、最高、平均等。您可以尝试以下方法来搜索最低的关键字:

$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);

希望这有帮助:)

您可以使用
range()
array\u diff
以及
array\u 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);
然后,如果只想找到一个打开的插槽,可以将该数组中的第一个值用作最低值或任意值

在PHP中,是否有用于查找数组中没有值的最低数字键的函数

没有

是遍历数组并测试isset()的最简单方法

循环是可以的,但不是最好的选择。当数组键存在但包含
NULL
值时,它可能会引发误报。更好的选择是

在您的情况下,循环可以简单到:

还是有更简单的方法


查看其他答案,这些答案可能更简单,也可能不简单。

使用
范围生成所需的密钥,然后找到丢失的最小密钥<代码>$expected\u keys=范围(0,计数($your\u数组)-1)$缺少_键=数组_差异($expected_键,array_键($your_数组));返回最小值($缺少_键)+1表示简单方式,+1表示注释。我不想迂腐,但我认为你应该将
$key
重命名为
$keys
;)这比循环查找可用密钥更容易吗?嗨@salath-我想你是在问Shankar这个问题。我不认为这是必要的,我想有不止一种方法可以剥猫皮:)我个人不反对循环,这只是我的方法。@Darragh我在向你讲话,因为问题是问“最简单”的方法。我宁愿使用
min()
max()
来获得最小值和最大值(这样就不需要
sort()
)。
for ($key = 0; array_key_exists($key, $arr); $key++);
var_dump($key);