首页 > 代码库 > PHP 数组函数练习

PHP 数组函数练习

  1 <?php  2     header("content-type:text/html;charset=uft-8");  3     $arr = array(  4         a => 1,  5         B => 2,  6     );  7     $arr = array_change_key_case($arr,CASE_UPPER);  8     echo "array_change_key_case: CASE_UPPER ...CASE_LOWER the same as CASE_UPPER<pre>";  9     print_r($arr); 10     echo "</pre><br/>"; 11  12     $arr = array( 13         a => 1, 14         v => 2, 15         c => 3, 16         d => 4, 17         e => 5, 18     ); 19     //对数组进行分块,是否保留原有的key,array_chunk($inputs,$size[,bool $preserve_key])  20     echo "array_chunk: <pre>"; 21     print_r(array_chunk($arr, 2 , true)); 22     echo "</pre><br/>"; 23  24     //对key和value进行组合,两个数组的个数要相同,不然会爆warning 25     $a = array(1,2,3,4); 26     $b = array(a,b,c,d); 27     echo "array_combine: <pre>"; 28     print_r(array_combine($a, $b)); 29     echo "</pre><br/>"; 30  31     //对数组中的value进行计算 32     $arr = array( 33         a => 1, 34         v => 2, 35         c => 3, 36         d => 4, 37         e => 5, 38         as => 1, 39     ); 40     echo "array_count_values: <pre>"; 41     print_r(array_count_values($arr)); 42     echo "</pre><br/>"; 43  44     //对数组进行差集计算,计算出包含在$a中,而不在其他参数数组中的值,判断条件为 (string)elem1 == (string)elem2 45     $a = array(1,2,3,4); 46     $b = array(1,2); 47     echo "array_diff: <pre>"; 48     print_r(array_diff($a,$b)); 49     echo "</pre><br/>"; 50  51     //对数组进行差集计算,计算出包含在$a中,而不在其他参数数组中的值,判断条件为 (string)key1 == (string)key2,(string)values1 == (string)values2 52       $a = array("red" => 1,"black" => 2 , 1 => 2); 53     $b = array("red" => 1, "1" => "3"); 54     echo "array_diff_assoc: <pre>"; 55     print_r(array_diff_assoc($a,$b)); 56     echo "</pre><br/>"; 57  58     //对数组进行差集计算,计算出包含在$a中,而不再其他参数数组中的值,判断条件为 (string)key1 == (string)key2 59     $a = array("red" => 1,"black" => 2 , 1 => 2); 60     $b = array("red" => 1, "1" => "3"); 61     echo "array_diff_key: <pre>"; 62     print_r(array_diff_key($a, $b)); 63     echo "</pre><br/>"; 64  65     //对数组进行差集计算,计算出包含在$a中,而不在其他参数数组中的值,判断条件为 (string)key1 == (string)key2,(string)values1 == (string)values2 66     function my_array_diff_uassoc($a,$b){ 67         if($a == $b){ 68             return 0; 69         } 70         return $a > $b ? 1 : -1; 71     } 72     $a = array("red" => 1,"black" => 2 , 1 => 2); 73     $b = array("red" => 1, "1" => "2"); 74     echo "array_diff_uassoc :<pre>"; 75     print_r(array_diff_uassoc($a,$b,"my_array_diff_uassoc")); 76     echo "</pre><br/>"; 77  78     //对数组进行差集计算,计算出包含在$a中,而不再其他参数数组中的值,判断条件为 (string)key1 == (string)key2 79     function my_array_diff_ukey($a,$b){ 80         if($a == $b){ 81             return 0; 82         } 83         return $a > $b ? 1 : -1; 84     } 85     $a = array("red" => 1,"black" => 2 , 1 => 2); 86     $b = array("red" => 1); 87     $c = array("1" => "2"); 88     echo "array_diff_ukey: <pre>"; 89     print_r(array_diff_ukey($a, $b,$c,"my_array_diff_ukey")); 90     echo "</pre><br/>";     91  92     //array_fill ,对数组给定的值  array_fill($start,$size,$value);  $start => 下标开始位置,$size给定数组大小,$value =http://www.mamicode.com/> 数值 93     echo "array_fill: <pre>"; 94     print_r(array_fill(1,10,"banana")); 95     echo "</pre><br/>"; 96  97     //array_fill_keys,组合一个数组,key可以自己定义,every key => $value .... 98     echo "array_fill_keys:<pre>"; 99     $keys = array(hello,2,3,foo);100     $value = http://www.mamicode.com/array(1,2,3,4);101     print_r(array_fill_keys($keys,111));102     echo "</pre><br/>";103 104     //array_filter,自定义一个过滤函数,根据过滤的值得到返回参数105     function my_array_filter($a){106         return $a % 2;107     }108     $arr = array(1,2,3,0);109     echo "array_filter: <pre>";110     print_r(array_filter($arr,my_array_filter));111     print_r(array_filter($arr));112     echo "</pre><br/>";113 114     //array_flip,将数组的key 和 value进行交换,3 => 5, 4 => 5会覆盖115     $arr = array(1 => 2,3 => 5,4 => 5);116     echo "array_flip: <pre>";117     print_r(array_flip($arr));118     echo "</pre><br/>";119 120     //array_intersect,返回一个包含在$a中,同时出现中其他数组中的值121     $a = array(1,2,3,4);122     $b = array(1);123     $c = array(1,2,3);124     echo "array_intersect: <pre>";125     print_r(array_intersect($a,$b,$c));126     echo "</pre><br/>";127 128     //array_intersect_assoc,返回一个包含在$a中,同时出现在其中数组中的值 判断条件为(string)key1 == (string)key2 , (string)value1 ==  (string)value2129      $a = array(1,2,3,4);130     $b = array(1);131     $c = array(0 => 1,2,3);132     echo "array_intersect_assoc: <pre>";133     print_r(array_intersect_assoc($a,$b,$c));134     echo "</pre><br/>";135 136     //array_intersect_key,返回一个包含在$a中同时出现在其他数组中key对应的值,(string)key1 == (string)key2137     $a = array(a,b,c,d);138     $b = array(1);139     $c = array(2);140     echo "array_intersect_key: <pre>";141     print_r(array_intersect_key($a,$b,$c));142     echo "</pre><br/>";143 144     //array_intersect_uassoc 与array_intersect()不同的是,key用于回调函数,(string)value1 == (string)value2145     $a = array(red => a,"black" => 2, 333 => 2);146     $b = array(Red => a);147     echo "array_intersect_uassoc: <pre>";148     print_r(array_intersect_uassoc($a, $b, strcasecmp));149     echo "</pre><br/>";150 151     //array_intersect_ukey  用回调函数来比较key152     $a = array(red => a,black => 2);    153     $b = array(RED => b);154     echo "array_intersect_key: <pre>";155     print_r(array_intersect_ukey($a, $b , strcasecmp));156     echo "</pre><br/>";157 158     //array_keys  array_keys($a,5,true),$a 只返回 keys, search_value 返回对应value的key , bool strict,是否严格159     $a = array(2,3,4,5,5);160     echo "array_keys: <pre>";    161     print_r(array_keys($a,5,true));162     echo "</pre><br/>";163 164     //array_map165     function my_array_map($a){166         return $a * $a * $a;167     }168     $a = array(2,3,4,5,5);169     echo "array_map: <pre>";170     print_r(array_map(my_array_map,$a));171     echo "</pre><br/>";172 173     //array_merge174     $a = array("0" => "red",re => 1);175     $b = array("0" => "yellow","re" => 2);176     echo "array_merge: <pre>";177     print_r(array_merge($a,$b));178     echo "</pre><br/>";179 180     //array_merge_recursive181     $ar1 = array("color" => array("favorite" => "red"), 5);182     $ar2 = array(10, "color" => array("favorite" => "green", "blue"));183     $result = array_merge_recursive($ar1, $ar2);184     echo "array_merge_recursive: <pre>";185     print_r($result);186     echo "</pre><br/>";187 188     //array_multisort 对多维数组进行排序,或者多个数组排序189     $data[] = array(volume => 67, edition => 2);190     $data[] = array(volume => 86, edition => 1);191     $data[] = array(volume => 85, edition => 6);192     $data[] = array(volume => 98, edition => 2);193     $data[] = array(volume => 86, edition => 6);194     $data[] = array(volume => 67, edition => 7);195     $v = array();196     $e = array();197     foreach ($data as $key => $value) {198         $v[] = $value[volume];199         $e[] = $value[edition];200     }201     array_multisort($v,SORT_ASC,$e,SORT_ASC,$data);202     echo "array_multisort: <pre>";203     print_r($data);204     echo "</pre><br/>";205 206     //array_pad   一个数组填充函数,对数组进行填充,array_pad($inputs,$size,$pad_value) ....size > 0 ,在右侧填充,size < 0 在左侧填充, abs($size) > count($inputs) 否侧不填充207     $arr = array(2,3,4);208     $inputs = array_pad($arr,-10,"no");209     echo "array_pad: <pre>";210     print_r($inputs);211     echo "</pre><br/>";212 213     //array_pop ....出栈,长度减去1,返回出栈元素214     $arr = array(1,2,4);215     echo "array_pop: <pre>";216     print_r(array_pop($arr));217     echo "</pre><br/>";218 219 220     //array_product 计算数组中元素的乘积221     $arr = array(1,2,3,4);222     echo "array_product: <pre>";223     print_r(array_product($arr));224     echo "</pre><br/>";225 226     //array_sum 计算数组中元素的和227     $arr = array(1,2,3,4);228     echo "array_sum: <pre>";229     print_r(array_sum($arr));230     echo "</pre><br/>";231 232     //array_push 入栈,返回数组单元总数233     $arr = array(1,2,3,4);234     echo "array_psuh: <pre>";235     print_r(array_push($arr, 6,5));236     echo "</pre><br/>";237 238     //array_rand 随机抽取数组中的size个 array_rand($inputs,$size)   $inputs为数组239     $arr = array(1,2,3,4);240     echo "array_rand: <pre>";241     print_r(array_rand($arr,count($arr) - 1));242     echo "</pre><br/>";243 244     //array_reduce 用回调函数迭代将数组简化成单一的值245     $arr = array(1,2,3);246     function get_sum($v,$m){247         return $v + $m;248     }249     echo "array_reduce: <pre>";250     print_r(array_reduce($arr,get_sum));251     echo "</pre><br/>";252 253     //array_replace 函数使用后面数组元素的值替换第一个 array 数组的值。如果一个键存在于第一个数组同时也存在于第二个数组,它的值将被第二个数组中的值替换。如果一个键存在于第二个数组,但是不存在于第一个数组,则会在第一个数组中创建这个元素。如果一个键仅存在于第一个数组,它将保持不变。如果传递了多个替换数组,它们将被按顺序依次处理,后面的数组将覆盖之前的值。 254     $arr = array("orange","ping","juice","apple");255     $replacement = array(111 => 222);256     echo "array_replace: <pre>";257     print_r(array_replace($arr,$replacement));258     echo "</pre><br/>";259 260     //array_reverse 将数组进行翻转261     $arr = array("1" => 2,2 => red);262     echo "array_reverse: <pre>";263     print_r(array_reverse($arr));264     echo "</pre><br/>";265 266     //array_search 查找值是否在数组中267     $arr = array(222 => 111);268     echo "array_search: <pre>";269     print_r(array_search(111,$arr,true));270     echo "</pre><br/>";271 272     //array_shift 将数组开头的单元移出数组,返回第一个元素273     $arr = array(a,2,3);274     echo "array_shift: <pre>";275     print_r(array_shift($arr));276     echo "</pre><br/>";277 278     //array_slice 从数组中提取一部分 array_slice($arr,$offset,$length[,bool $preserve_key]);   279     $inputs = array(a,b,4 => c,d);280     echo "array_slice: <pre>";281     print_r(array_slice($inputs,2,2,true));282     echo "</pre><br/>";283 284     //array_splice 用数组去代替原数组中的一部分 array_splice($inputs,$offsets[[,$length,],$resplacement])285     $input = array("red", "green", "blue", "yellow");286     array_splice($input, 1 , 2,dsadsa);287     echo "array_splice: <pre>";288     print_r($input);289     echo "</pre><br/>";290 291     //array_unique 当且仅当 (string)value1 == (string)value2,保留第一个值292     $arr = array(1,2,3,2,3);293     echo "array_unique: <pre>";294     var_dump(array_unique($arr));295     echo "</pre><br/>";296 297     //array_shift 在数组头加入一个或者多个单元298     $arr = array(2,3,4);299     $add = array(5,6);300     array_unshift($arr, $add);301     echo "array_unshift: <pre>";302     print_r($arr);303     echo "</pre><br/>";304 305     //array_walk 对数组中的每个成员应用用户函数306     function my_array_walk(&$v,$k,$str){307         $v = $v . $k . $str;308     }    309     $arr = array(red,black,blue,green);310     echo "array_walk: <pre>";311     array_walk($arr,my_array_walk,sss);312     print_r($arr);313     echo "</pre><br/>";314 315     //array_walk_recursive 对数组中的每个成员应用用户函数316     function my_array_walk_recursive($v,$k){317         echo "$k holds $v<br/>";318     }319     $arr = array(red => 1,black => 2);320     $bigarr = array(haha => $arr,pink => 3);321     array_walk_recursive($bigarr, my_array_walk_recursive);322 323 ?>

自己练习而已,不喜勿喷。

PHP 数组函数练习