-
-
- #Change the case of array keys
- $arr1=array("a"=>"Lamp","db"=>"database","LANGUAGE"=> "PHP");
- print_r(array_change_key_case($arr1,CASE_UPPER));
- echo "
";
- print_r(array_change_key_case($arr1,CASE_LOWER));
- echo "
";
-
- #Split an array into multiple third parameters to set whether to retain keys
- $arr2=array('a','b','c','d','e', 'f','g');
- print_r(array_chunk($arr2,2,true));
- echo "
";
- print_r(array_chunk($arr2,2,false));
- echo "
";
-
- #array array_diff_assoc ( array $array1 , array $array2 [, array $ ... ] ) returns an array,
- #This array includes all the items in array1 but Values not in any other parameter array
- #Different keys count
-
- $arr3=array('a'=>'green','b'=>'brown','c'=>'red' );
- $arr4=array('a'=>'green','yellow','red');
- print_r(array_diff_assoc($arr3,$arr4));
- echo "
";
-
- #array_diff ( array $array1 , array $array2 [, array $ ... ] )
- #Returns an array that includes everything in array1 but not in any
- #Other parameter arrays Different values and keys are not counted
- print_r(array_diff($arr3,$arr4));
- echo "
";
-
- #array_fill ( int $start_index , int $num , mixed $value )
- #Fill an array with num entries using the value of the value parameter,
- #The key name starts with the start_index parameter.
- print_r(array_fill(-5,8,"banana"));
- echo "
";
-
-
- #array_flip ( array $trans )
- #Return a reversed array , for example, the key name in trans becomes the value,
- #And the value in trans becomes the key name.
- $arr5=array('a'=>'1',"b"=>"2","c","d","e");
- print_r(array_flip($arr5));
- echo "
";
-
- #array_map ( callback $callback , array $arr1 [, array $... ] )
- #Return an array that contains all cells in arr1 The unit after the callback has been applied to
- #. The number of parameters accepted by callback should be consistent with the number of arrays passed to array_map()
- #function.
- function cube($n){
- return $n*$n;
- }
- $arr6=array(1,2,3,4,5);
- print_r(array_map("cube",$arr6));
- echo "
";
-
- #array_merge_recursive ( array $array1 [, array $... ] )
- #Merge the cells of one or more arrays, and append the values in one array After the previous array
- #. Returns the resulting array. If the input arrays have the same string key name,
- # then the values will be merged into an array, which will go on recursively, so if a value itself
- # is an array, this function will follow the corresponding entry Merge it into another array. However, if
- #if the arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to
- #.
- $arr7=array("color"=>array("favorite"=>"red"),5);
- $arr8=array(10,array("favorite"=>"yellow","blue "));
- print_r(array_merge_recursive($arr7,$arr8));
- echo "
";
-
- #array_reduce ( array $input , callback $function [, int $initial ]
- #Apply the callback function iteratively to each unit in the input array, thereby simplifying the array
- # into a single value. If the optional parameter initial is specified, the parameter will be treated as the
- #th element in the array. A value to process, or as the final return value if the array is empty. If the array is empty and no initial parameter is passed, array_reduce() returns NULL
- function rsum($v,$w){
- $v+. =$w;
- return $v;
- }
- function rmul($v,$w){
- $v*=$w;
- return $v;
- }
- $a=array(1,2,3,4 ,5);
- $x=array();
- $b=array_reduce($a,"rsum");
- $c=array_reduce($a,"rmul",10);
- $d=array_reduce($x ,"rsum",1);
- echo $b."tt".$c."tt".$d."n";
- echo "
";
-
- #array_replace ( array &$array , array &$array1 [, array &$array2 [, array &$... ]] )
- #Function replaces the value of the first array array with the value of the subsequent array element if a key exists. The first
- # array also exists in the second array, and its value will be replaced by the value in the second array. If a
- # key exists in the second array, but not in the first array, This
- # element will be created in the first array.If a key only exists in the first array, it will remain unchanged. If multiple replacement number
- #groups are passed, they will be processed in order, and subsequent arrays will overwrite the previous values.
-
- $base=array("orange","banana","apple","raspberry");
- $replacements=array(0=>"pineapple",4=>"cherry");
- $replacements2 =array(0=>"grape");
- #print_r(array_replace($base,$replacements,$replacements2));
- #echo "
";
-
- #array_splice ( array &$input , int $offset [, int $length [, array $ replacement ]] )
- #Remove the units specified by offset and length in the input array. If the replacement
- # parameter is provided, use the replacement array Unit replacement. Returns an array containing the removed cells
- # . Note that numeric key names in input are not preserved. If length is omitted, all parts of the array from
- # offset to the end are removed. If length is specified and is positive, this many cells are removed
- # . If length is specified and is a negative value, all elements from offset to length
- # inverse of the end of the array are removed. Tip: When replacement is given and you want to remove all cells from offset to
- # the end of the array, use count($input) as length.
-
- $input=array("red","green","blue","yellow");
- array_splice($input,1,-1);
- print_r($input);
- echo "
";
-
- #key ( array &$array )
- #Return the key name of the current unit in the array.
- $fruit=array("fruit1"=>"apple","fruit2"=>"orange","fruit3"=>"grape",
- "fruit4"=>"apple","fruit5" =>"apple");
- while($fruit_name=current($fruit)){
- if($fruit_name=='apple'){
- echo key($fruit)."
";
- }
- next($fruit);
- }
- echo "
";
- ?>
Copy code
|