数组函数详解 1.count()和 sizeof()统计数组元素个数
$arr=array(1,2,3,4,5);
echo count($arr),PHP_EOL;
echo sizeof($arr); 输出显示5
5 2.each() 返回当前元素的键名和键值,并将内部指针向前移动
$arr=array(1,2,3,4,5);
var_dump(each($arr)); 输出显示array(4) {
[1]=>
int(1)
["value"]=>
int(1)
[0]=>
int(0)
["key"]=>
int(0)
} 3.list() 用于给一组变量赋值
$arr=array(1,2,3,4,5);
list($a,$b,$c,$d)=$arr;
echo "a$a,b$b,c$c,d$d";
a1,b2,c3,d4 4.数组元素的排序:sort()、asort()和 ksort()都是正向排序,rsort()、arsort()和 krsort()是反向排序按键值正向排序 sort()、asort()
按键名正向排序 ksort()
按键值反向排序 rsort()、arsort()
按键名反向排序 krsort()
$arr=array(12,32,43,47,51,21,37,24,95);
sort($arr);
var_dump($arr);
rsort($arr);
var_dump($arr);
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
foreach($age as $key=>$value){
echo "Key=".$key.",Value=".$value,PHP_EOL;
} 输出显示
按键值正向排序 sort()
array(9) {
[0]=>
int(12)
[1]=>
int(21)
[2]=>
int(24)
[3]=>
int(32)
[4]=>
int(37)
[5]=>
int(43)
[6]=>
int(47)
[7]=>
int(51)
[8]=>
int(95)
}按键值反向排序 rsort()
array(9) {
[0]=>
int(95)
[1]=>
int(51)
[2]=>
int(47)
[3]=>
int(43)
[4]=>
int(37)
[5]=>
int(32)
[6]=>
int(24)
[7]=>
int(21)
[8]=>
int(12)
}按键名正向排序 ksort()
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35按键名反向排序 krsort()
Key=Peter,Value=35
Key=Joe,Value=43
Key=Ben,Value=375.array_count_values()统计重复数组的个数
$arr=array(12,12,43,43,51,12,37,24,43);
var_dump(array_count_values($arr)); 输出显示array(5) {
[12]=>
int(3)
[43]=>
int(3)
[51]=>
int(1)
[37]=>
int(1)
[24]=>
int(1)
} 6.current():显示数组中的第一个元素
$arr=array(32,43,47,51,21,37,24,95);
$age=array("Ben"=>"37","Peter"=>"35","Joe"=>"43");
echo current($arr),PHP_EOL;
echo current($age); 输出显示32
37 7.array_pad($,10,0); 数组首尾选择性追加 10代表总的元素个数
$arr=array(32,43,47,51,21,37,24,95);
var_dump(array_pad($arr,10,1)); 输出显示array(10) {
[0]=>
int(32)
[1]=>
int(43)
[2]=>
int(47)
[3]=>
int(51)
[4]=>
int(21)
[5]=>
int(37)
[6]=>
int(24)
[7]=>
int(95)
[8]=>
int(1)
[9]=>
int(1)
} 8.unset() 删除变量
$arr=32;
unset($arr);
echo $arr; 输出显示报错:没有找到这个变量
Notice: Undefined variable: arr in C:\phpStudy\PHPTutorial\WWW\my\zuoye\12.11.php on line 26 9.array_fill() 创建数组
用法一:
$arr=array(3);//创建两个元素为3的数组
var_dump(array_fill(1,2,$arr));
用法二:
print_r($arr=array_fill(1,10,"0"));//生成十个元素为0的数组 输出显示
用法一:
array(2) {
[1]=>
array(1) {
[0]=>
int(3)
}
[2]=>
array(1) {
[0]=>
int(3)
}
用法二:
Array (
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
)10.array_combine() 通过合并两个数组来创建一个新数组,其中的一个数组元素为键名,另一个数组元素为键值
$arr=array("a","b","c");
$aee=array("1","2","3");
$c=array_combine($arr,$aee);
print_r($c); 输出显示Array
(
[a] => 1
[b] => 2
[c] => 3
) 11.array_splice()删除数组成员
$arr=array("a","b","c","d");
array_splice($arr,2); //删除从当前数以后的所有数组
print_r($arr); 输出显示Array
(
[0] => a
[1] => b
) 12.array_unique删除数组中的重复值
$arr=array("a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d");
print_r(array_unique($arr)); 输出显示Array
(
[0] => a
[1] => b
[2] => c
[3] => d
) 13.array_flip()交换数组的键值和值
$arr=array("a"=>"A","b"=>"B","c"=>"C","d"=>"D");
print_r(array_flip($arr)); 输出显示Array
(
[A] => a
[B] => b
[C] => c
[D] => d
) 14.array_search()搜索数值所在的位置
$arr=array("a","b","c","d");
echo array_search("d",$arr); 输出显示
3
|