PHP array functions

Source: Internet
Author: User
Tags shuffle sorts

$arr Array (1,2,3,4,5,6, "a" =>7, "B" =>8, "C" =>9,1, "haha");


Returns all the values of the array, returning the array

Var_dump (array_values($arr));


Returns all the keys of the array, returning the array

Var_dump (array_keys($arr));




Detects whether a value is contained in the array. return True, False
Parameters: Value to query, array, true (= = =)/False (= =) Default

Var_dump (in_array$arr,true));


Swaps the keys and values in the array, returning the new array

Var_dump (array_flip($arr));




Reverses the array. Returns the new array.
Parameters:

① need to invert the array,
②true: Preserves the matching of the subscript and value of the original indexed array (values and keys, while reversing).
False: Only the value is flipped, and the key is not flipped. Default.
Regardless of the true/false, the associative array is not affected, the associative array key, the value is always a pair

Var_dump (array_reverse($arr,true));




Count the number of array elements

Count ($arr);




Counts the number of occurrences of all values in the array. Returns a new array
New array format: The value of the original array (the value after the reset)
The number of times the value corresponding to the original array appears.

Var_dump (array_count_values($arr));


Remove duplicate values from the divisor group!

    Var_dump (array_unique($arr));


Filter each value in the array:
① does not pass the callback function: filter out all null values (0/""/null/false/"0"/[])
② callback function: You need to pass a parameter to the callback function to determine if the parameter meets the requirements, and if so, return true; otherwise, return false;

Var_dump (array_filter($arr,function($num) {if($num>4 ) {returntrue;} Else {returnfalse;}});

Each value of an array is processed by a callback function. (Directly modifies the original array and returns whether the bool type is successful)
When executed, the callback function is passed two parameters, which are the value,key of the array, and then the values and keys can be processed in the callback function!
But!!! When it comes to modifying values, you must pass the address &!!!!!

$fruits=Array("D" = "lemon", "a" and "Orange", "B" and "Banana", "C" and "Apple");Var_dump(Array_walk($fruits,function(&$item,$key,$num){Echo"{$key}-->{$item}<br> ";$item.=$key;$item.=$num;}, "10"));Var_dump($fruits);


/Each value of the array is referred to the callback function for mapping processing.

Array_map (): The first parameter, which is a callback function. The second parameter is the >=1 number group. The

has several arrays that can pass several arguments to the callback function, representing a value for each array,

can handle the value, and return by return after processing, so the corresponding value of the new array is the value you return.

"Array_map and array_walk similarities and differences"
Same point: can iterate through the array, through the callback function, each value of the array is re-processed;
Differences: ①walk can only pass an array, the callback function receives the value and key of the array;
Map can pass multiple arrays, the callback function receives the value of each array, the
②walk directly modifies the original array, and map does not modify the original array, the new array is returned;
③walk can pass a remainder parameter to the callback function, map can only pass the value of the array;
④ processing mode, Walk if you need to get rid of the value of the original array, you need to pass the address in the callback function, directly modify the value of the variable;
and map, you can modify the value of the new array by returning the new value with return.

$a = [1,2,3,4,5]; $b = [1,2,3,4,5]; //     $arr 1 = array_map (function ($value) {//    return $value *2;//    }, $a); $arr 1 Array_map (function($value 1,$value 2) {return$value 1+ $value 2 ;} $a,$b); Var_dump ($arr 1);




Sort-Sorts an array (ascending): You can pass in the second argument and control what sort. The second argument is 1, which means sort by number. The second argument is 2, which means sort by string, ASCII code! It is automatically detected by default.

Rsort-Reverse array sorting (descending)

Usort--sorts the values in the array using a user-defined comparison function
Usort ($arr 1,function ($a, $b) {
//return $a- $b; Ascending
Return $b-$a;//Descending
});

>>> The following 3 functions, commonly used for associative array sorting. Use the same as above
Asort-sort the array and keep the index relationship
Arsort-reverse-sort the array and keep the index relationship
Uasort--user-defined comparison function sorts the array and keeps the index association


Ksort --Sort the array by key name
Krsort--Reverse-sort an array by key name
Uksort--Sort the key names in the array using a user-defined comparison function

Natural sort: Numbers follow 0-9, the letters are sorted by a-Z;
The following two functions are sorted by nature, and key-value associations are preserved when sorting.
Natsort-Sort an array with the "natural sort" algorithm
Natcasesort--sort the "natural sort" algorithm for an array of case-insensitive letters


Array_multisort--sorts multiple arrays or multidimensional arrays.
First parameter: The first array (required) is followed by an optional parameter: Sort_desc/sort_asc (ascending descending), sort_string/sort_numberic (Sort by number or string)
, and then, after that, is a plurality of optional arrays;

Collation: The first array, followed by the array, in accordance with the first array corresponding to the relationship, a column of a column of the movement!!!

If you sort multiple arrays, you need to keep multiple number of leader degrees consistent. Otherwise, a warning is reported

  $arr 2  = [6,9,4,7,3,8  $arr 3  = [6,9,4,7,3,8  $score  = [89,12,78,45,3  $name  = ["Zhangsan", "Lisi", "Wangermazi", "Liergou", "Niesanfen" ";  array_multisort  ( $score , Sort_desc,sort_numeric, $name  Span style= "color: #000000");  var_dump  ( $score   var_dump  ( $name ); 

Array array_slice (array array, int offset [, int length [, bool Preserve_keys]])
① Array, you must
② from the beginning of the first interception, must be selected. A negative number indicates that several from the right, and (in the default order of the array, contains the associative, indexed array instead of subscript)
③ the length of the Intercept, optional. Default, cut to the last;
Whether the ④boolean class maintains key-value associations is optional. Default, Index reorder, true to keep index associated

$arr 1 Array_slice ($arr, 2,5,true); Var_dump ($arr 1);

Array Array_splice array &$input$offset$lengthmixed$replacement ])


Return value: the deleted array;
Parameters:
① the address of an array, modifies the original array
② from the beginning of the first delete, replace;
③ the length of the deleted or replaced
The ④ is empty, which indicates the delete operation; The incoming content represents the new value of the replacement operation;

$arr = Array (1,2,3,4,5,6, "a" =>7, "B" =>8, "C" =>9,1, "haha");

$arr 1 Array_splice ($arr, 4,5,[0,0,0,0,0,0]); Var_dump ($arr 1); Var_dump ($arr);


Array_combine-Creates an array with the value of an array as its key name, and the value of another array as its value
Array_combine (an array of keys, as an array of values);
Two arrays must be consistent, otherwise a warning is returned, false.

$arr 1 = [1,2,3,4,5]; $arr 2 = [6,7,8,9,0]; $arr 3 Array_combine ($arr 1$arr 2); Var_dump ($arr 3)

Merging arrays
Array Array_merge (array array1 [, array array2 [, array ...])
Merges multiple arrays, and then tiles the following array behind the preceding array.
If an association key value with the same name appears in multiple arrays, the previous is overwritten.

$arr 1 = [=>4, "a", "a", "ten" =>5]; $arr 2 = [6,7,8, "a" =>9, "ten" =>0]; $arr 3 Array_merge ($arr 1,$arr 2); Var_dump ($arr 3);


Array Fetch intersection

Array Array_intersect Array Array array ...] )

Multiple arrays are intersected, and the result preserves the key-value Association match for the first array.

$arr 1 = [1,2,9, "a" =>4,5]; $arr 2 = [6,7,8, "a" =>9, "ten" =>5]; $arr 3 Array_intersect ($arr 1$arr 2); Var_dump ($arr 3);


Array difference Set

Array Array_diff Array Array array ...] )


Takes out multiple arrays, including in the first array, but does not contain values in other arrays. Retains the key value Association of the first array.

$arr 1 = [1,2,9, "a" =>4,5]; $arr 2 = [6,7,8, "a" =>9, "ten" =>5]; $arr 3 Array_diff ($arr 1$arr 2); Var_dump ($arr 3);

Array_pop: Deletes the last value of the array and returns the value;
Array_push: At the end of the array, put 1-Multiple values, returning the number of elements of the array after processing.

Array_shift: Deletes the first value of the array and returns the value;
Array_unshift: Array begins with 1-multiple values, returning the number of elements of the array after processing.

$arr 1 = [1,2,3,4,5,6,7]; // Echo Array_pop ($arr 1);//echo Array_push ($arr 1,[8,9],0);//echo array_shift ($arr 1); Echo Array_unshift ($arr 1, 0); Var_dump ($arr 1);


Array_rand: Randomly extract 1 or more key names from the array!! The second argument is empty, which means that one is drawn, and the incoming number indicates that n is pumped.

$arr 1 = [1,2,3,4,5,6,7]; Var_dump (array_rand($arr 1, 3));

Shuffle: Randomly disrupts array order and modifies the original array directly.

$arr 1 = [1,2,3,4,5,6,7]; Shuffle ($arr 1); Var_dump ($arr 1);

$arr 1 = Array (1,2,3,4,5,6, "a" =>7, "B" =>8, "C" =>9,1, "haha", "haha", 3,5);
$arr 2 = array ();
1, there is an empty array arr2: The value of the original array to remove the weight value--the number of occurrences of each value of the original array
2, traverse the original array arr1: And take out each of the arr1 values;
3, Detection: The new value is taken, whether in arr2, there is a key with the same name.
If you have: The description finds a value that is duplicated with the new value that is now being taken. Then, the value of this key in the arr2 corresponds to +1;
If not: Description up to now, no items have been duplicated with the new value. So, just create a new key with the same name in Arr2, with a value of 1;

foreach($arr 1  as $key 1=$value 1) {$isHas=false;foreach($arr 2  as $key 2=$value 2) {if($key 2==$value 1){$arr 2[$value 1] ++;$isHas=true;}}if(!$isHas)$arr 2[$value 1] = 1;}Var_dump($arr 2);





PHP array functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.