Reprint PHP Array (i)

Source: Internet
Author: User
Tags sorts
Reprint PHP Array (1)

Original: http://batwyx.iteye.com/blog/343090


PHP array is an important concept, it contains a lot of functions, convenient for people to develop ... The array is now sorted to make it easy to query and apply.
First, the definition of the PHP array ... The PHP array contains two items, key and value, which can be used to obtain the corresponding value, where key can be numeric and associative, such as $array[0], $array [one] ...
Create an array
The array declaration in PHP is a little bit different from other languages, but it can be declared as one-dimensional, two-dimensional, three-dimensional, multidimensional, etc.
$array [0] = 1, $array = Array (n/a); A one-dimensional array, which consists of only three values, is a numeric array, can be referenced with $array[0] to represent 1, and the index is omitted when creating a numeric array.
$array = Array (
?? 1 = "One",
?? 2 = "a",
?? 3 = "three",
?? 4 = Array (
??? "One" = 1,
??? "Both" = 2,
??? "Three" = 3
?? )
);
A two-dimensional array, which is also an associative array, can be $array[4]["one" to represent 1 when referenced.
More than three-dimensional ...
If you are creating an array in bulk, you can use the following function:
Array range (mixed low, mixed high [, number step])
such as $array = Range (1,6); represents array (1,2,3,4,5,6);
?? $array = range (a,f); Represents an array (a,b,c,d,e,f);

Output array
There are more functions in the output array in PHP, which are commonly used
BOOL Print_r (mixed expression [, bool return])
void Var_dump (mixed expression [, mixed expression [, ...]])
There are also like echo,print,printf that can output a single array.

Test array
Sometimes we need to decide if a variable is an array, then you can use:
BOOL Is_array (mixed Var)

Adding or removing array elements
Array declarations are not immutable, and can be further manipulated by adding deletions to arrays:
int Array_push (array &array, mixed var [, mixed ...]) presses one or more cells into the end of the array, and the length of the array increases according to the number of variables in the stack, such as Array_push ($array, $var)
Mixed Array_pop (array &array) pops the last element of the array (out of the stack) and resets the array's pointer after the end
Mixed Array_shift (array &array) returns the first element of the array.
int Array_unshift (array &array, mixed var [, mixed ...]) inserts one or more cells at the beginning of the array
Arrays Array_pad (array input, int pad_size, mixed pad_value) fill the array with values to a specified length, such as Array_pad ($array, 3, $var);

Locating array elements
BOOL In_array (mixed needle, array haystack [, BOOL strict]) checks if a value exists in the array
Array Array_keys (array input [, mixed search_value [, BOOL Strict]]) returns all the key names in the array and re-composes a new array
BOOL Array_key_exists (mixed key, array search) checks to see if the given key exists in the array.
Array array_values (array input) returns all the values in the array
Mixed Array_search (mixed needle, array haystack [, bool strict]) searches the array for the given value, and succeeds returns key.

Iterating through an array
PHP provides a lot of functions to get key and value
Mixed key (array &array) gets the key name from the associative array
Mixed reset (array &array) resets the arrays pointer
Array each (array &array) returns the key/value pairs in the array and moves the array forward one step
Mixed current (array &array) returns the cell in the array
Mixed end (array &array) moves the pointer in the array to the last
Mixed next (array &array) moves the pointer in the array down one
Mixed prev (array &array) moves the pointer in the array up one
Array array_reverse (array array [, BOOL Preserve_keys]) returns a cell in reverse order
Array Array_flip (array trans) swaps the key-value roles in the array
In addition to the above functions, you can use loops to iterate over elements in an array, such as
foreach (array_expr as $value)
{Statement???}
foreach (array_expr as $key = $value)
{statement??}
Extracts each key/value pair until all items are obtained or some internal conditions are met
void list (mixed varname, mixed ...) assign the values in the array to some variables

Determining array size and uniqueness
int count (mixed var [, int mode]) calculates the number of attributes in an array of cells or objects in the arrays, and sizeof's function with the same name
Array array_count_values (array input) counts the number of occurrences of all values in the array
Array array_unique (array array) to remove duplicate values from the divisor group

Array sorting
This hearing is the core of the calculator problem ... Oh... The fact is that ...
BOOL Sort (array &array [, int sort_flags]) sorts the array
BOOL Natsort (array &array) sorting arrays by natural sorting method
BOOL Natcasesort (array &array) sorts the array by natural sorting, not case-sensitive
BOOL Rsort (array &array [, int sort_flags]) reverse-order the array
BOOL Asort (array &array [, int sort_flags]) sorts the array and maintains the index relationship
BOOL Array_multisort (array ar1 [, mixed arg [, mixed ... [, array ...]] ) to sort multiple arrays or multidimensional arrays
BOOL Arsort (array &array [, int sort_flags]) sorts the array in reverse order and keeps the index relationship
BOOL Ksort (array &array [, int sort_flags]) ordering of array key names
BOOL Krsort (array &array [, int sort_flags]) reverse order of array key names

Merging, splitting, joining, and decomposing arrays
Array array_combine (array keys, array values) creates an array with the value of an array as its key name, and the value of the other array as its value
Array Array_merge (array array1 [, array array2 [, array ...]]) merges one or more arrays
Array array_merge_recursive (array array1 [, array ...]) recursively all one or more arrays
Array array_slice (array array, int offset [, int length [, bool Preserve_keys]]) takes a paragraph out of the array, creates a new array, and if offset is positive, splits the array switch from the distance Offset position starts, if negative, the split starts at offset from the end of the array, at which point the count (Input_array)-|length| position of the array switch ends
Array Array_splice (array &input, int offset [, int length [, array replacement]]) removes some of the values from the array and replaces them with other values. Offset setting Ibid.
Array Array_intersect (array array1, array array2 [, array ...]) computes the intersection of the array, that is, if the value that appears in the first array appears in the next few arrays, the value is taken out
Array Array_intersect_assoc (array array1, array array2 [, array ...]) with the intersection in the Index check array
Array Array_intersect_key (array array1, array array2 [, array ...]) compares the intersection of the array with the key name
Array Array_diff (array array1, array array2 [, array ...]) computes the difference set of the array, that is, the different values in the first array
Array Array_diff_assoc (array array1, array array2 [, array ...]) with index check the difference set in the array
Array Array_diff_key (array array1, array array2 [, array ...]) compares the difference set in the array using the key name

Other more useful array functions
There are many array functions that are not listed ... A few more useful and more often, the other on the reference manual ... It's clear in the handbook.
Mixed Array_rand (array input [, int num_req]) randomly takes out one or more keys in an array, num specifies the number of
BOOL Shuffle (array &array) disrupts the array
Number Array_sum (array array) computes the sum of all the values in the array, and the associative array ignores
Array array_chunk (array input, int size [, bool Preserve_keys]) divides an array into several

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