php 中常用数组操作 数组分割 次数 键名差集
本教程收藏了大量的php初学者要用到的数组实例,包括有创建数组用前两个数组值作为新数组的键与值 数据返回成字符串 将数组分割,不保留原数组键名把原数组元素出现次数赋值给新数组并显示,对键名计算差集等数据操作,实例代码如下:
-
- $array=array(1,1,1,1,1,8=>1,4=>1,19,3=>13);
- print_r($array);
-
- $a=array('green','red','yellow');
- $b=array('avocado','apple','banana');
- $c=array_combine($a,$b);
- print_r($c);
-
- foreach(range(0,12)as $number)
- {
- echo $number.",";
- }
- echo "<p>";
- foreach(range(0,100,10)as $number)
- {
- echo $number.",";
- }
- echo "<p>";
- foreach(range('a','i')as $letter)
- {
- echo $letter.",";
- }
- echo "<p>";
- foreach(range('c','a')as $letter)
- {
- echo $letter.",";
- }
-
- $input_array=array('a','b','c','d','e');
- print_r(array_chunk($input_array,2));
- print_r(array_chunk($input_array,2,true));
-
- $array=array(1,"hello",1,"php","hello");
- print_r(array_count_values($array));
-
- $array1=array("a"=>"green","b"=>"brown","c"=>"blue","red");
- $array2=array("a"=>"green","yellow","red");
- $result=array_diff_assoc($array1,$array2);
- print_r($result);
-
- $array1=array('blue'=>1,'red'=>2,'green'=>3);
- $array2=array('green'=>4,'blue'=>5,'yellow'=>6);
- $result=var_dump(array_diff_key($array1,$array2));
- print_r($result);
-
-
- function key_compare_func($a, $b)
- {
- if($a==$b)
- {
- return 0;
- }
- return($a>$b)?1:-1;
- }
-
- $array1=array("a"=>"green","b"=>"brown","c"=>"blue","red");
- $array2=array("a"=>"green","yellow","red");
-
- $result=array_diff_uassoc($array1,$array2,"key_compare_func");
- print_r($result);