文档库 最新最全的文档下载
当前位置:文档库 › PHP数组函数

PHP数组函数

PHP数组函数
PHP数组函数

1、array -- 新建一个数组

array array ( [mixed ...] )

返回根据参数建立的数组。参数可以用=> 运算符给出索引。

$array = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13);

print_r($array);

?>

2、in_array -- 检查数组中是否存在某个值

bool in_array ( mixed needle, array haystack [, bool strict] )

在haystack 中搜索needle,如果找到则返回TRUE,否则返回FALSE。

如果第三个参数strict 的值为TRUE 则in_array() 函数还会检查needle 的类型是否和haystack 中的相同。

注意: 如果needle 是字符串,则比较是区分大小写的。

3、shuffle -- 将数组打乱

bool shuffle ( array &array )

本函数打乱(随机排列单元的顺序)一个数组。

注意: 本函数为array 中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。

$numbers = range(1,20);

srand((float)microtime()*1000000);

shuffle($numbers);

foreach ($numbers as $number) {

echo "$number ";

}

?>

4、range -- 建立一个包含指定范围单元的数组

array range ( mixed low, mixed high [, number step] )

range() 返回数组中从low 到high 的单元,包括它们本身。如果low > high,则序列将从high 到low。

新参数: 可选的step 参数是PHP 5.0.0 新加的。

如果给出了step 的值,它将被作为单元之间的步进值。step 应该为正值。如果未指定,step 则默认为1。

// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

foreach (range(0, 12) as $number) {

echo $number;

}

// The step parameter was introduced in 5.0.0

// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) foreach (range(0, 100, 10) as $number) {

echo $number;

}

// Use of character sequences introduced in 4.1.0

// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'); foreach (range('a', 'i') as $letter) {

echo $letter;

}

// array('c', 'b', 'a');

foreach (range('c', 'a') as $letter) {

echo $letter;

}

相关文档