查询

array_is_list()函数—用法及示例

「 用于检查一个数组是否是一个索引数组(即连续的整数作为键名),并且键名从0开始依次递增 」


array_is_list() 函数用于检查一个数组是否是一个索引数组(即连续的整数作为键名),并且键名从0开始依次递增。

用法:

bool array_is_list ( array $array )

参数:

  • $array:需要检查的数组。

返回值:

  • 如果指定的数组是一个索引数组并且键名从0开始依次递增,返回 true
  • 如果指定的数组不是一个索引数组或者键名不是连续的整数,返回 false

示例:

// 示例1:索引数组
$example1 = [1, 2, 3, 4, 5];
$result1 = array_is_list($example1);
echo $result1 ? '是一个索引数组' : '不是一个索引数组';

// 示例2:关联数组
$example2 = ['a' => 1, 'b' => 2, 'c' => 3];
$result2 = array_is_list($example2);
echo $result2 ? '是一个索引数组' : '不是一个索引数组';

// 示例3:非连续的索引数组
$example3 = [2 => 'a', 4 => 'b', 6 => 'c'];
$result3 = array_is_list($example3);
echo $result3 ? '是一个索引数组' : '不是一个索引数组';

// 示例4:空数组
$example4 = [];
$result4 = array_is_list($example4);
echo $result4 ? '是一个索引数组' : '不是一个索引数组';

输出结果:

是一个索引数组
不是一个索引数组
不是一个索引数组
是一个索引数组
补充纠错
上一个函数: array_intersect_ukey()函数
下一个函数: array_keys()函数
热门PHP函数
分享链接