- #改变数组键的大小写
- $arr 1=array ("a" = "Lamp", "db" = "database", "LANGUAGE" and "PHP");
- Print_r (Array_change_key_case ($arr 1,case_upper));
- echo "
";
- Print_r (Array_change_key_case ($arr 1,case_lower));
- echo "
";
- #将一个数组分割成多个 the third parameter sets whether the key is reserved
- $arr 2=array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ');
- Print_r (Array_chunk ($arr 2,2,true));
- echo "
";
- Print_r (Array_chunk ($arr 2,2,false));
- echo "
";
- #array Array_diff_assoc (array $array 1, array $array 2 [, array $ ...]) returns an array,
- #该数组包括了所有在 Array1 but not the values in any other parameter array
- #键不同也算
- $arr 3=array (' a ' + = ' green ', ' b ' = ' brown ', ' c ' = ' red ');
- $arr 4=array (' a ' = ' green ', ' yellow ', ' red ');
- Print_r (Array_diff_assoc ($arr 3, $arr 4));
- echo "
";
- #array_diff (array $array 1, array $array 2 [, array $ ...])
- #返回一个数组, the array includes all of the array1 but not any
- #其它参数数组中的值, different keys are not counted
- Print_r (Array_diff ($arr 3, $arr 4));
- echo "
";
- #array_fill (int $start _index, int $num, mixed $value)
- The value of the #用 value parameter populates an array with num entries,
- #键名由 the start specified by the Start_index parameter.
- Print_r (Array_fill ( -5,8, "banana"));
- echo "
";
- #array_flip (Array $trans)
- #返回一个反转后的 array, such as the key name in trans, becomes a value,
- The value in the #而 trans is the key name.
- $arr 5=array (' a ' = ' 1 ', "b" = "2", "C", "D", "E");
- Print_r (Array_flip ($arr 5));
- echo "
";
- #array_map (callback $callback, array $arr 1 [, Array $ ...]
- #返回一个数组, the array contains all the cells in the arr1 that have been callback
- #之后的单元. The number of parameters accepted by callback should be passed to Array_map ()
- #函数的数组数目一致.
- function cube ($n) {
- return $n * $n;
- }
- $arr 6=array (1,2,3,4,5);
- Print_r (Array_map ("Cube", $arr 6));
- echo "
";
- #array_merge_recursive (array $array 1 [, Array $ ...])
- #将一个或多个数组的单元合并起来, the value in an array is appended to the previous array
- #的后面. Returns an array as the result. If the input array has the same string key name,
- #则这些值会被合并到一个数组中去, this will recursively go down, so if a value itself
- #是一个数组, this function merges it into another array according to the corresponding entry. However, as
- #果数组具有相同的数组键名, the latter value will not overwrite the original value, but is appended to the
- #后面.
- $arr 7=array ("Color" =>array ("favorite" = "Red"), 5);
- $arr 8=array (10,array ("favorite" = "yellow", "blue"));
- Print_r (Array_merge_recursive ($arr 7, $arr 8));
- echo "
";
- #array_reduce (array $input, callback $function [, int $initial]
- The #将回调函数 function iterates through each cell in the input array, thus the array
- #简化为单一的值. If an optional parameter, initial, is specified, the parameter will be treated as an array of
- #一个值来处理, or, if the array is empty, as the final return value. If the array is empty and no
- #有传递 the initial parameter, 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. " \t\t ". $c." \t\t ". $d." \ n ";
- echo "
";
- #array_replace (array & $array, array & $array 1 [, Array & $array 2 [, Array &$ ...])
- #函数使用后面数组元素的值替换第一个 the values of array arrays. If a key exists in the first
- #个数组同时也存在于第二个数组, its value will be replaced by the value in the second array. If a
- #键存在于第二个数组, but does not exist in the first array, this is created in the first array.
- #元素. If a key exists only in the first array, it will remain unchanged. If more than one number of replacements are passed
- #组, they will be processed sequentially, followed by an array that overrides the previous value.
- $base =array ("Orange", "banana", "apple", "raspberry");
- $replacements =array (0=> "Pineapple",4=> "cherry");
- $replacements 2=array (0=> "Grape");
- #print_r (Array_replace ($base, $replacements, $replacements 2));
- #echo "
";
- #array_splice (array & $input, int $offset [, int $length [, array $ replacement]])
- #把 input array is removed by the unit specified by offset and length, if replacement is provided
- # parameter is replaced with a cell in the replacement array. Returns an array containing the cells that have been removed
- # 。 Note the numeric key names in input are not retained. If length is omitted, the group is removed from the
- # Offset to the end of all parts. If length is specified and positive, so many cells are removed
- # 。 If length is specified and is negative, remove the inverse length from offset to the end of the array
- # So far in the middle of all the units. Tip: When replacement is given, remove the offset to
- # when all the cells at the end of the array, count ($input) as length.
- $input =array ("Red", "green", "blue", "yellow");
- Array_splice ($input, 1,-1);
- Print_r ($input);
- echo "
";
- #key (Array & $array)
- #返回数组中当前单元的键名.
- $fruit =array ("fruit1" = "Apple", "fruit2" and "Orange", "fruit3" and "Grape",
- "FRUIT4" = "Apple", "fruit5" and "Apple";
- while ($fruit _name=current ($fruit)) {
- if ($fruit _name== ' Apple ') {
- echo Key ($fruit). "
";
- }
- Next ($fruit);
- }
- echo "
";
- ?>
Copy Code
|