Array functions in PHP learning record 2

Source: Internet
Author: User
1. Check whether the given key name or index exists in the array--array_key_exists

Usage: array_key_exists ($key (mixed), $input (array)) returns True and False

$input _array=array ("1" = "Java", "op" = "R", "act" = "python", "PHP", "Redis");
$array =array ("SD", "WD", "GH");
Var_dump (array_key_exists (1, $array));
Var_dump (Array_key_exists ("Op", $input _array));

Result: BOOL (TRUE) bool (true)

2. Returns all the key names in the array--array_keys

Usage: Array_keys ($input (array)) returns the key names in the array

$input _array=array ("1" = "Java", "op" = "R", "act" = "python", "PHP", "Redis");
$array =array ("SD", "WD", "GH");
Var_dump (Array_keys ($array));
Var_dump (Array_keys ($input _array));

Result: Array (3) {[0]=> int (0) [1]=> int (1) [2]=> int (2)}

Array (5) {[0]=> int (1) [1]=> string (2) "Op" [2]=> string (3) "Act" [3]=> int (2) [4]=> int (3)}

3. Function The callback function on the cell of the given array array_map

Usage: Array_map (function,array1 (array), array2 ...) Feel this and filter function Array_filter understand a bit similar, array_map can be understood to be a processing function, the processing method writes itself.

function cube ($n) {
Return ($n * $n * $n);
}
$a = Array (1, 2, 3, 4, 5);
Var_dump (Array_map ("Cube", $a));

Result: Array (5) {[0]=> int (1) [1]=> int (8) [2]=> int ([4]=>) [3]=> int (125)}

4, merging one or more groups-array_merge (is to seek union)

Syntax: Appends a value from one array to another, and if it is a numeric index, the key name is re-indexed in a sequential way

$input _array=array ("1" = "Java", "op" = "R", "act" = "python", "PHP", "Redis");
$array =array ("SD", "WD", "GH");
Var_dump (Array_merge ($input _array, $array));

Result: Array (8) {[0]=> string (4) "Java" ["Op"]=> string (1) "R" ["Act"]=> string (6) "Python" [1]=> string (3) "PHP" [2]=> string (5) "Redis" [3]=> string (2) "SD" [4]=> string (2) "WD" [5]=> string (2) "GH"}

Re-index key value Java key name changed from 1 to 0

5. Merge one or more arrays recursively--array_merge_recursive

Usage: As with Array_merge, just Array_merge is the union, and Array_merge_recursive is the real 1+1=2, the key value repetition does not overwrite, the key name repeats automatically merges

$ar 1 = Array ("color" = = Array ("Favorite" = "red"), 5,10);
$ar 2 = Array ("color" = = Array ("Favorite" = "green", "Blue"));
Var_dump (Array_merge_recursive ($ar 1, $ar 2));

Result: Array (4) {["Color"]=> Array (2) {["Favorite"]=> Array (2) {[0]=> string (3) "Red" [1]=> string (5) "Green"} [0]=> string (4) "Blue"} [0]=> int (5) [1]=> Int (ten) [2]=> int (10)}

6. Sort multiple arrays or multidimensional arrays-array_multisort

Usage: Array_multisort (array1,array2 ...), returns TRUEon success, or FALSEon failure.

$ar 1 = Array ("Ten", 101, +, "a");
$ar 2 = Array (1, 3, "2", 1);
Array_multisort ($ar 1, $ar 2);
Var_dump ($ar 1);
Var_dump ($ar 2);

Results:

Array (4) {[0]=> string (2) "Ten" [1]=> string (1) "a" [2]=> int (+) [3]=> int (101)} array (4) {[0]=> int (1) [1]=> Int (1) [2]=> string (1) "2" [3]=> int (3)}

7. Fill the array with a value to the specified length--array_pad

Usage: Array_pad ($input (array), $size (int), mixed) Returns the result of the fill

$array 1=array (10,20,30);
$array 2=array (' java ' =>array (' Redis ', ' MySQL ');
Var_dump (Array_pad ($array 1, 6, ' 40 '));
Var_dump (Array_pad ($array 2,4, ' shell '));

Results:

Array (6) {[0]=> int (ten) [1]=> int (2) [2]=> int (in) [3]=> string (2) "" [4]=> string () "Max" [5]=> str ING (2) "40"}

Array (4) {["Java"]=> Array (2) {[0]=> string (5) "Redis" [1]=> string (5) "MySQL"} [0]=> string (5) "Shell" [1] = = String (5) "Shell" [2]=> string (5) "Shell"}

8. Eject the last unit of the array (out of the stack)--array_pop

Usage: Array_pop ($input (array)) returns the last cell of the array, or nullif the array is empty (or not an array).

$array 1=array (10,20,30);
$array 2=array ();
Var_dump (Array_pop ($array 1));
Var_dump (Count ($array 1));
Var_dump (Array_pop ($array 2));

Result: Int (int.) int (2) (array length minus 1) NULL

9. Calculate the product--array_product of all the values in the array

Usage: array_product ($array), (all numbers are required in the array)

$array 1=array (10,20,30);
$array 2=array (1, ' er ');
$array 3=array (10,20,30=>20);
Var_dump (Array_product ($array 1));
Var_dump (Array_product ($array 2));
Var_dump (Array_product ($array 3));

Result: Int (6000) int (0) int (4000)

10. Press one or more cells into the end of the array (into the stack)--array_push

Usage: Array_push ($array, mixed ...) This is the array_pop out of the stack corresponding to append to the tail, return the number of arrays

$array 1=array (10,20,30);
Var_dump (Array_push ($array 1, ' erer ', ' Weert '));
Var_dump ($array 1);

Result: Int (5) Array (5) {[0]=> int (ten) [1]=> int ([2]=> int) [3]=> string (4) "Erer" [4]=> string (5) "Weer T "}

11. Randomly remove one or more cells from the array--array_rand

Usage: Array_rand ($array, $num _rep (int)) randomly extracts multiple cells from an array (returns a key name)

$array 1=array (10,20,30,40,50,60);
Var_dump (Array_rand ($array 1,3));

Result: Array (3) {[0]=> int (0) [1]=> int (1) [2]=> int (4)}

12. iteratively use the callback function to simplify the array to a single value--array_reduce

Usage: array_reduce ($input (array), Function,int), iterates the callback function functions into each cell in the input array, simplifying the array to a single value. If an optional parameter initial is specified, the parameter will be treated as the first value in the array, or if the array is empty, as the final return value

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 ();
Var_dump (Array_reduce ($a, "rsum"));
Var_dump (Array_reduce ($a, "Rmul", 10));
Var_dump (\array_reduce ($x, "Rsum", 1));

Result: Int (+) int (+) int (1)

13. Replace the elements of the first array with the passed array recursion--array_replace_recursive

Usage: array_replace_recursive (array1,array2,array3 ...) Replaces the value of the first array with the value of the array element that follows. If a key exists in the first array and also exists in the second array, its value is replaced by the value in the second array. If a key exists in the second array, but does not exist in the first array, the element is created in the first array. If a key exists only in the first array, it will remain unchanged. If multiple alternate arrays are passed, they are processed sequentially, followed by an array that overrides the previous value.

14. Replace the element of the first array with the passed array--array_replace

Usage: array_replace (array1,array2,......) The function replaces the value of the first array array with the value of the element that follows it. If a key exists in the first array and also exists in the second array, its value is replaced by the value in the second array. If a key exists in the second array, but does not exist in the first array, the element is created in the first array. If a key exists only in the first array, it will remain unchanged. If multiple alternate arrays are passed, they are processed sequentially, followed by an array that overrides the previous value.

$base = Array (' citrus ' = = Array ("Orange"), ' berries ' = = Array ("BlackBerry", "Raspberry"),);
$replacements = Array (' citrus ' = = Array (' pineapple '), ' berries ' = = Array (' blueberry '));
$basket = Array_replace_recursive ($base, $replacements);
Print_r ($basket);
$basket = Array_replace ($base, $replacements);
Print_r ($basket);

Result: Array ([citrus] = = Array ([0] = pineapple) [berries] = = Array ([0] = = Blueberry [1] = = Raspberry) )

Array ([citrus] = = Array ([0] = pineapple) [berries] = = Array ([0] = Blueberry))

15. Returns an array of cells in reverse order--array_reverse

Usage: array_reverse ($array, <参数> ) parameter true to preserve the original key name

$input  = Array ("PHP", 4.0, Array ("Green", "red"), ' a ' = ' B ');
Var_dump (Array_reverse ($input));
Var_dump (Array_reverse ($input, TRUE));

Result: Array (4) {["a"]=> string (1) "B" [0]=> Array (2) {[0]=> string (5) "Green" [1]=> string (3) "Red"} [1]=> Float (4) [2]=> string (3) "PHP"}

Array (4) {["a"]=> string (1) "B" [2]=> Array (2) {[0]=> string (5) "Green" [1]=> string (3) "Red"} [1]=> Flo at (4) [0]=> string (3) "PHP"}

16, search the array for the given value, if successful returns the corresponding key name--array_search, the failure returns false

Usage: array_search (mixed, $array)

$input  = Array (1=> "PHP", 2=> ' java ',3=> ' MySQL ', ' a ' = ' B ');
Var_dump (Array_search (' php ', $input));
Var_dump (Array_search (' B ', $input));

Result: Int (1) string (1) "a"

17. Move the cell at the beginning of the array to a group--array_shift acting like a pointer

Usage: array_shift (array); Moves the first cell of the array out and returns as a result, reducing the length of the array and moving all other cells forward one bit. All numeric key names are changed from zero to start, and the text key name is unchanged. If array is empty (or not an array), null is returned

$input  = Array (1=> "PHP", 2=> ' java ',3=> ' MySQL ', ' a ' = ' B ');
Var_dump (Array_shift ($input));
Var_dump ($input);

Result: String (3) "PHP"

Array (3) {[0]=> string (4) "Java" [1]=> string (5) "MySQL" ["a"]=> string (1) "B"}

18. Remove a section of--array_slice from the array

Usage: array_slice (array,offset (int), length (int), ), if the argument is true, the key of the array is not re-made

$input = Array ("A", "B", "C", "D", "E");
Print_r (Array_slice ($input, 2,-1));
Print_r (Array_slice ($input, 2,-1, true));

Result: Array ([0] = c [1] + D) Array ([2] = c [3] = + D)

19. Remove part of the array and replace--array_splice with other values

Array_splice ($input (array), $offset (int), $length (int), $replace (array))

Remove the cells specified by offset and length from the input array and replace them with the cells in the replacement array if the replacement parameter is provided. Returns an array containing the cells that have been removed. Note the numeric key names in input are not retained. If offset is positive, the offset specified by the value in the input array begins to be removed. If offset is negative, it is removed from the offset specified by the value of the countdown at the end of input.

If length is omitted, all parts of the array from offset to end are moved. If length is specified and positive, so many cells are removed. If length is specified and negative, all cells from offset to the end of the array ending with length are removed. Tip: When replacement is given to remove all cells from offset to the end of the array, count ($input) is used as length.

If the replacement array is given, the removed cell is replaced by the cells in this array. If the combination of offset and length does not remove any values, the cells in the replacement array are inserted into the position specified by offset. Note the key names in the replacement array are not preserved. If the value used to replace is just a unit, you do not need to add an array () to it, unless the cell itself is an array.

 
  $input 1 = Array ("Red", "green", "blue", "yellow");
Array_splice ($input 1, 2);
Var_dump ($input 1);
$input 2 = Array ("Red", "green", "blue", "yellow");
Array_splice ($input 2, 1,-1);
Var_dump ($input 2);
$input 3 = Array ("Red", "green", "blue", "yellow");
Array_splice ($input 3, 1, COUNT ($input 3), "orange");
Var_dump ($input 3);
$input 4 = Array ("Red", "green", "blue", "yellow");
Array_splice ($input 4,-1, 1, Array ("Black", "maroon"));
Var_dump ($input 4);
$input 5 = Array ("Red", "green", "blue", "yellow");
Array_splice ($input 5, 3, 0, "purple");
Var_dump ($input 5);

Array (2) {[0]=> string (3) "Red" [1]=> string (5) "Green"}

Array (2) {[0]=> string (3) "Red" [1]=> string (6) "Yellow"}

Array (2) {[0]=> string (3) "Red" [1]=> string (6) "Orange"}

Array (5) {[0]=> string (3) "Red" [1]=> string (5) "Green" [2]=> string (4) "Blue" [3]=> string (5) "Black" [4]=&G T String (6) "Maroon"}

Array (5) {[0]=> string (3) "Red" [1]=> string (5) "Green" [2]=> string (4) "Blue" [3]=> string (6) "Purple" [4]=& Gt String (6) "Yellow"}

20. Calculate and--array_sum all values in the array

Usage: Array_num (array) (number)

 
  $a = Array (2, 4, 6, 8);
$b = Array ("a" = = 1.2, "b" = = 2.3, "c" = 3.4);
Var_dump (Array_sum ($a));
Var_dump (Array_sum ($b));

Result: Int (a) float (6.9)

  • 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.