go语言interface接口继承多态示例及定义解析
1186
2022-09-11
数组函数详解(常见的数组函数)
$arr=array(1,2,3,4,5);
echo count($arr),PHP_EOL;
echo sizeof($arr);
输出显示
5
5
$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)
}
$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=37 |
$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)
}
$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
$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)
}
$arr=32;
unset($arr);
echo $arr;
输出显示
报错:没有找到这个变量
Notice: Undefined variable: arr in C:\phpStudy\PHPTutorial\WWW\my\zuoye\12.11.php on line 26
用法一:
$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 ) |
---|---|
$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
)
$arr=array("a","b","c","d");
array_splice($arr,2); //删除从当前数以后的所有数组
print_r($arr);
输出显示
Array
(
[0] => a
[1] => b
)
$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
)
$arr=array("a"=>"A","b"=>"B","c"=>"C","d"=>"D");
print_r(array_flip($arr));
输出显示
Array
(
[A] => a
[B] => b
[C] => c
[D] => d
)
$arr=array("a","b","c","d");
echo array_search("d",$arr);
输出显示
3
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~