An array of PHP learning summaries

Source: Internet
Author: User
Tags compact pears php programming php programming language shuffle

Overview

We know that in the PHP programming language, arrays are used very often, and almost every script will use them. PHP comes with a large number of excellent operation of the array of functions for us to use, this article on the use of these array functions to do some classification and summary, convenient for you to check later.

Create

1.range()

Create an array of the specified range:

$arr 1 = range (0, ten);     # Array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ten) $arr 2 = range (0, ten, 2);  # Array (0, 2, 4, 6, 8, ten) $arr 3 = range (' A ', ' d ');  # array (' A ', ' B ', ' C ', ' d ') $arr 4 = range (' d ', ' a ');  # array (' d ', ' C ', ' B ', ' a ')

2.compact()

Create an array that contains the variable names and their values:

$number = ten; $string = "I m phper"; $array  = Array ("and", "you?"); $result = Compact ("Number", "string", "array"); # Array (' number ' =>10, ' string ' = "I ' m phper", ' Array ' =>array ("and", "You?"))

3.array_combine()

Create an array that uses the value of one array as its key, and the value of another array as its value:

$key    = Array ("1", "3", "5", "7", "9"), $value  = Array ("I", "Am", "A", "PHP", "er"), $result = Array_combine ($number, $array);     # array (' 1 ' =>i, ' 3 ' = ' Am ', ' 5 ' = ' A ', ' 7 ' = ' PHP ', ' 9 ' = ' er ')

Traverse

1. for Cycle

$arr = Range (0), for ($i = 0; $i < count ($arr);  $i + +) {    echo $arr [$i];}
disadvantage: Only indexed arrays can be traversed.

2. while Cycle

$products = Array (' Apple ' =>3, ' milk ' =>6, ' eggs ' =>10); (List ($product, $quantity) = each ($products)) {    Echo $product. '-' . $quantiry;}
disadvantage: After the traversal is complete, the second traversal of the array is not possible (the pointer to the last element is pointed to the inside of the arrays).

3. foreach Cycle

$products = Array (' Apple ' =>3, ' milk ' =>6, ' eggs ' =>10); foreach ($products as $product = = $quantity) {    echo $product. '-' . $quantiry;}

Operation Key or value

unset()-delete array members or arrays
in_array()-check if a value exists in the array
array_key_exists()-Checks whether the given key name or index exists in the array
array_search()-Searches for the given value in the array and returns the corresponding key name if successful

$array = Array (1, 2, 3); unset ($array); # array () $fruit = Array (' apple ' = ' goold ', ' orange ' = ' fine ', ' banana ' = ' OK '), if (In_array (' good ', $fruit)) {
  echo ' Exit ';} $search _array = Array (' first ' = = 1, ' second ' + 4), if (array_key_exists (' first ', $search _array)) {    echo "Exit"; } $array = Array (0 = ' Blue ', 1 = ' red ', 2 = ' green ', 3 = ' red '); $key = Array_search (' green ', $array); # $key = 2;

array_keys()-returns some or all of the key names in the array
array_values()-Returns all values in the array

$array  = Array (' apple ' = ' good ', ' orange ' = ' fine ', ' banana ' = ' OK '); $keys   = Array_keys ($array);   # array (' Apple ', ' orange ', ' banana ') $values = Array_values ($array); # array (' Good ', ' fine ', ' OK ')

array_unique()-Remove duplicate values from the divisor group

$input  = Array (4, ' 4 ', ' 3 ', 4, 3, ' 3 '); $result = Array_unique ($input); # Array (4, ' 3 ')

array_flip()-Swap keys and values in the array

$input  = Array (' Oranges ', ' apples ', ' pears '); $result = Array_flip ($input); # array (' Oranges ' =>0, ' apples ' =>1 , ' Pears ' =>2)

array_count_values()All values in the statistics array

$input  = Array (1, ' Hello ', 1, ' World ', ' hello '); $result = Array_count_values ($input); # array (' 1 ' =>2, ' hello ' = >2, ' World ' =>1)

Sort

1. sort() andrsort()

To sort an array in ascending or descending order:

$fruits = Array (); sort ($fruits);  # array (' Apple ', ' banana ', ' lemon ', ' orange ') rsort ($fruits); # array (' orange ', ' lemon ', ' banana ', ' apple ')

2. asort() andarsort()

Sort the associative array (by the value of the element) in ascending or descending order and keep the index relationship:

$fruits = Array (' d ' = ' lemon ', ' a ' = = ' orange ', ' b ' = ' banana ', ' c ' = ' apple '); Asort ($fruits);  # array (' c ' + = ' apple ', ' b ' = ' = ' banana ', ' d ' = ' lemon ', ' a ' = = ' orange ') arsort ($fruits); # array (' a ' = = ' orange ', ' d ' = ' = ' lemon ', ' b ' = ' ' banana ', ' c ' = ' apple ')

3.ksort()

The array is sorted by key name:

$fruits = Array (' d ' = ' lemon ', ' a ' = = ' orange ', ' b ' = ' banana ', ' c ' = ' apple '); Ksort ($fruits); # array (' a ' = ' = ' orange ', ' b ' = = ' banana ', ' c ' = ' apple ', ' d ' = ' lemon ')

4.shuffle()

Randomly scrambled array sorting:

$numbers = Range (1, 5); shuffle ($numbers); # Array (3, 2, 5, 1, 4)

Stacks and queues

array_push()-Press one or more cells into the end of the array (into the stack)
array_pop()-pops the last element of the array (out of the stack)

$stack = Array (' Orange ', ' banana '); Array_push ($stack, ' apple ', ' raspberry '); # array (' Orange ', ' banana ', ' apple ', ' raspberry ') $fruit = Array_pop ($stack);  #array (' orange ', ' banana ', ' apple ')

array_unshift()-Insert one or more cells at the beginning of the array
array_shift()-Moves the cell at the beginning of the array to the group

$queue = Array (' Orange ', ' banana '); Array_unshift ($queue, ' apple ', ' raspberry '); # array (' Apple ', ' raspberry ', ' orange ', ' banana ') $fruit = Array_shift ($queue); # array (' Raspberry ', ' orange ', ' banana ')

Split, fill, merge

array_slic()-Remove a paragraph from the array
array_splice()-Remove part of the array and replace it with other values

$input  = Array (' A ', ' B ', ' C ', ' d ', ' e '); $result = Array_slice ($input, 2); # array (' C ', ' d ', ' e ') $input = Array (' Red ', ' Green ', ' blue ', ' yellow '); Array_splice ($input, 2, 1); # Array (' Red ', ' green ', ' yellow ')

array_pad()-fills a value into an array with the specified length

$input  = Array (9), $result = Array_pad ($input, 5, 0);   # Array (9, 0, 0) $result = Array_pad ($input,-7,-1); # Array (-1,-1,-1,-1, 12, 10, 9)

array_fill()-fills the array with the given value

$a = Array_fill (5, 3, ' a ');     # Array (5=> ' a ', 6=> ' a ', 7=> ' a ') $b = Array_fill ( -2, 3, ' pear '); # Array ( -2=> ' a ', 0=> ' a ', 1=> ' a ')

array_fill_keys()-Fills an array with the specified key and value

$keys   = Array (' foo ', 5, ' Bar '); $result = Array_fill_keys ($keys, ' a '); # array (' foo ' = ' a ', 5=> ' a ', 10=> ' a ', ' bar ' = ' a ')

array_merge()-Merging one or more arrays

$array 1 = Array (' data0 '), $array 2 = Array (' data1 '); $result = Array_merge ($array 1, $array 2); # array (' data0 ', ' data1 ')

Other functions

1.array_walk()

Use the user-defined function to do callback processing for each element in the array (changing the original arrays):

$a = Array (1, 2, 3, 4, 5); Array_walk ($a, function (& $value, $key) {    + + $value;}); # Array (2, 3, 4, 5, 6)

2.array_map()

Function The callback function on the cell of the given array (without altering the original array and generating a new array as the result):

$a = Array (1, 2, 3, 4, 5); $b = Array_map (function ($item) {    return $item + 1;}, $a); # Array (2, 3, 4, 5, 6)

3.array_rand()

Randomly fetching one or more elements from an array:

$input  = Array (' Apple ', ' banana ', ' lemon ', ' orange '); $result = Array_rand ($input, 2); # Array (' banana ', ' lemon ')

4.array_diff()

Computes the difference set of the array value:

$array 1 = Array (' a ' = = ' green ', ' red ', ' blue ', ' red '), $array 2 = array (' b ' = ' = ' green ', ' yellow ', ' red '); $result = arr Ay_diff ($array 1, $array 2); # array (' Blue ')

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.