Summary of array and access methods in PHP

Source: Internet
Author: User
Tags random seed
1. Key name and value array_values ($ arr) of the Basic Function Array for Array Operations; array_keys ($ arr) of the array; array_flip ($ arr) of the array key name ); values in the array are exchanged with key names (if there is a duplicate, the previous one will be overwritten) in_array (apple, $ arr); Retrieve applearray_search (apple, $ ar) in the array

1. Key name and value array_values ($ arr) of the Basic Function Array for Array Operations; array_keys ($ arr) of the array; array_flip ($ arr) of the array key name ); values in the array are exchanged with key names (if there is a duplicate, the previous one will be overwritten) in_array ("apple", $ arr); search for apple array_search ("apple ", $ ar

I. basic functions of Array Operations

Key name and value of the array

Array_values ($ arr); obtain the array value
Array_keys ($ arr); get the key name of the array
Array_flip ($ arr); values in the array are exchanged with key names (if there is a duplicate, the previous one will be overwritten)
In_array ("apple", $ arr); search for apple in the array
Array_search ("apple", $ arr); search for apple in the array. If a return key name exists
Array_key_exists ("apple", $ arr); retrieves whether the specified key name exists in the array.
Isset ($ arr [apple]): retrieves whether a given key name exists in the array.

Internal pointer of Array

Current ($ arr); returns the current unit in the array
Pos ($ arr); returns the current unit in the array
Key ($ arr); returns the key name of the current unit in the array
Prev ($ arr); returns the internal pointer in the array to one
Next ($ arr); move the internal pointer in the array one by one
End ($ arr); points the internal pointer in the array to the last unit.
Reset ($ arr; point the internal pointer in the array to the first unit
Each ($ arr); returns the structure of a key name/value of the current element of the array, and moves the array pointer one byte forward.
List ($ key, $ value) = each ($ arr); obtain the key name and value of the current element of the array.

Conversion between arrays and variables

Extract ($ arr) is used to convert elements in the array into variables and import them to the current file. The key name is used as the variable name and the value is used as the variable value.
Note: (the second parameter is very important. You can refer to the Manual for use) echo $;
Compact (var1, var2, var3); Create an array with the given variable name

Ii. Segmentation and filling of Arrays

Array Segmentation

Array_slice ($ arr,); removes a segment from the array. This function ignores the key name.
Array_splice ($ arr, array ("black", "maroon"); removes a segment of the array. Unlike the previous function, the returned sequence is deleted from the original array.

Split multiple arrays

Array_chunk ($ arr, 3, TRUE); you can divide an array into multiple values. TRUE indicates that the key name of the original array is retained.

Array Filling
Array_pad ($ arr, 5, 'x'); fill an array to the specified length.

3. array and stack

Array_push ($ arr, "apple", "pear"); pushes one or more elements to the end of the array stack (inbound stack) and returns the number of inbound stack Elements
Array_pop ($ arr); pops up the last element of the array stack (Out stack)

4. arrays and queues

Array_shift ($ arr); the first element in the array is removed and returned as a result (the length of the array is reduced by 1, the other elements are moved forward by one, and the number key name is changed to the zero technology, text key name unchanged)
Array_unshift ($ arr, "a", array (1, 2); insert one or more elements at the beginning of the array

V. Callback Functions

Array_walk ($ arr, 'function', 'word'); Use user functions to process each member in the array (the third parameter is passed to the callback function)
Array_mpa ("function", $ arr1, $ arr2); can process multiple arrays (their length should be the same when two or more arrays are used)
Array_filter ($ arr, "function"); Use the callback function to filter each element in the array. If the callback function is TRUE, the current element of the array is included in the returned result array, the key name of the array remains unchanged.
Array_reduce ($ arr, "function", "*"); convert to a single-value function (* as the first value of the array)

6. Sorting Arrays

Sort arrays by element values

Sort ($ arr); sort in ascending order (by which the second parameter is sorted) Ignore the array sorting of key names
Rsort ($ arr); sort the key names in ascending order (the second parameter is sorted in what way)
Usort ($ arr, "function"); sort the values in the array using the User-Defined comparison function (the function has two parameters, 0 indicates equal, A positive number indicates that the first value is greater than the second value, and a negative number indicates that the first value is smaller than the second value.) Ignore the array sorting of key names.
Asort ($ arr); sort the key names in ascending order (in which the second parameter is sorted)
Arsort ($ arr); sort by Ascending Order (by which the second parameter is sorted)
Uasort ($ arr, "function"); sort the values in the array using the User-Defined comparison function (the function has two parameters, 0 indicates equal, positive number indicates that the first key is greater than the second, negative number indicates that the first key is smaller than the second .)

Sort arrays by key name

Ksort ($ arr); sort by key name in the forward order
Krsort ($ arr); sort by key name in reverse order
Uksort ($ arr, "function"); sort the key names in the array using the User-Defined comparison function (the function has two parameters, 0 indicates equal, A positive number indicates that the first is greater than the second, and a negative number indicates that the first is smaller than the second)

Sort by natural sorting

Natsort ($ arr); Natural sorting (ignore key names)
Natcasesort ($ arr); Natural sorting (case insensitive, key names ignored)

VII. array Calculation

Sum of array elements
Array_sum ($ arr); calculates the sum of all elements in the array.

Array Merging
Array_merge ($ arr1, $ arr2); merges two or more arrays (the same string key name, followed by the previous one, the same number key name, followed by no overwriting operation, instead, it is appended to the back)
"+" $ Arr1 + $ arr2; only the last one is retained for the same key name
Array_merge_recursive ($ arr1, $ arr2); recursive merge operation. If the array contains the same string key name, these values are merged into an array. If a value is an array, It is merged into another array according to the corresponding key name. When the array has the same Array key name, the latter value will not overwrite the original value, but will be appended

Array difference set
Array_diff ($ arr1, $ arr2); returns the result array of the difference set.
Array_diff_assoc ($ arr1, $ arr2, $ arr3); returns the result array of the difference set and compares the key names.

Array Intersection
Array_intersect ($ arr1, $ arr2); returns an array of intersection results
Array_intersect_assoc ($ arr1, $ arr2); returns an array of intersection results and compares the key names.

8. Other array Functions

Range (); Create an array containing the specified range units

Array_unique ($ arr); Removes repeated values from the array. The original key name is retained in the new array.
Array_reverse ($ arr, TRUE); returns an array in the opposite unit order to the original array. If the second parameter is TRUE, the original key name is retained.
// Srand (float) microtime () * 10000000); Random Seed trigger
Array_rand ($ arr, 2); randomly retrieves one or more elements from the array.
Shuffle ($ arr); disrupt the order of Arrays
This type of function allows you to operate arrays and interact with them in multiple ways. The essence of arrays is to store, manage, and operate a group of variables.
PHP supports one-dimensional and multi-dimensional arrays, which can be created by a user or by another function. Some specific database processing functions can generate arrays from database queries, and some functions return arrays.
Array_change_key_case-returns an array of string keys in lowercase or uppercase.
Array_chunk-divide an array into multiple
Array_combine-creates an array and uses the value of one array as its key name, and the value of another array as its value
Array_count_values-count the number of occurrences of all values in the array
Array_diff_assoc-calculate the array's difference set with index check
Array_diff_key-use the key name to compare the difference set of the calculated Array
Array_diff_uassoc-use the callback function provided by the user to perform an index check to calculate the array's difference set
Array_diff_ukey-use the callback function to compare the difference set of the calculated array with the key name
Array_diff-calculate the difference set of the array
Array_fill_keys-Fill an array with values, specifying keys
Array_fill-fill the array with the given value
Array_filter-filter elements in the array using the callback function
Array_flip-swap the keys and values in the array
Array_intersect_assoc-intersection of arrays computed by checking Indexes
Array_intersect_key-use a key name to compare and calculate the intersection of Arrays
Array_intersect_uassoc-returns the intersection of arrays computed by checking indexes and compares indexes with callback functions.
Array_intersect_ukey-use the callback function to compare key names to calculate the intersection of Arrays
Array_intersect-calculates the intersection of Arrays
Array_key_exists-check whether the given key name or index exists in the array
Array_keys-returns all the key names in the array.
Array_map-calls the callback function to the unit of the given array.
Array_merge_recursive-recursively merge one or more Arrays
Array_merge-merge one or more Arrays
Array_multisort-sorts multiple arrays or multi-dimensional arrays.
Array_pad-fill the array with a value to the specified length
Array_pop-bring up the last unit of the array (Out stack)
Array_product-calculate the product of all values in the array
Array_push-Push one or more units to the end of the array (into the stack)
Array_rand-randomly retrieve one or more units from the array
Array_reduce-Use callback functions to iteratively simplify an array to a single value
Array_reverse-returns an array with the opposite unit order.
Array_search-search for a given value in the array. If the value is successful, the corresponding key name is returned.
Array_shift-Removes elements starting with an array from the array
Array_slice-extract a segment from the array
Array_splice-removes a part of the array and replaces it with other values.
Array_sum-calculates the sum of all values in the array
Array_udiff_assoc-calculate the difference set of the array with index check and use the callback function to compare data
Array_udiff_uassoc-calculate the difference set of the array with index check, and use the callback function to compare data and indexes.
Array_udiff-use the callback function to compare data to calculate the difference set of the array
Array_uintersect_assoc-returns the intersection of arrays computed by checking indexes and compares data with callback functions.
Array_uintersect_uassoc-returns the intersection of arrays computed by checking indexes and compares data and indexes using callback functions.
Array_uintersect-calculate the intersection of arrays and use the callback function to compare data
Array_unique-Remove repeated values from the array
Array_unshift-insert one or more units at the beginning of the array
Array_values-returns all values in the array.
Array_walk_recursive-recursively applies user functions to each member in the array
Array_walk-apply user functions to each member in the array
Array-create an array
Arsort-sorts arrays in reverse order and maintains the index relationship.
Asort-sorts arrays and maintains the index relationship
Compact-create an array, including variable names and their values
Count-calculates the number of units in the array or the number of attributes in the object.
Current-returns the current cell in the array.
Each-returns the current key/value pair in the array and moves the array pointer one step forward.
End-points the internal pointer of the array to the last unit.
Extract-import the variable from the array to the current symbol table
In_array-check whether a value exists in the array
Key-Get the key name from the associated array
Krsort-Sort arrays in reverse order by key name
Ksort-sort the array by key name
List-assign values in the array to some variables
Natcasesort-use the "natural sorting" algorithm to sort arrays by case-insensitive letters
Natsort-Sort arrays using the "natural sorting" Algorithm
Next-move the internal pointer in the array to a forward position.
Alias of pos-current ()
Prev-returns the internal pointer of the array to one position.
Range-create an array containing the specified range units
Reset-point the internal pointer of the array to the first unit
Rsort-reverse sorting of Arrays
Shuffle-disrupt the Array
Alias of sizeof-count ()
Sort-sort Arrays
Uasort-sort the values in the array using the User-Defined comparison function and maintain the index Association
Uksort-sort the key names in the array using the User-Defined comparison Function

Usort-sort the values in the array using the User-Defined comparison Function

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

The most flexible thing in php is the array. A lot of data is displayed in an array. Here we will sort out the array Traversal method, which can be selected as needed.
In PHP, arrays are divided into two types: Numerical index arrays and associated arrays.
The numeric index array is the same as the array in C language. The subscript is 0, 1, 2...
The subscript of the correlated array may be of any type, which is similar to the hash and map structures in other languages.

The following describes three methods to traverse joined arrays in PHP:

Method 1: foreach

$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'very well ',
'Running' => 'not good ');
Foreach ($ sports as $ key => $ value ){
Echo $ key. ":". $ value ."
";
?>

Output result:

Football: good
Faster Ming: very well
Running: not good

Method 2: each

$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'very well ',
'Running' => 'not good ');
While ($ elem = each ($ sports )){
Echo $ elem ['key']. ":". $ elem ['value']."
";
?>

Method 3: list & each

$ Sports = array (
'Football' => 'good ',
'Canonicalization' => 'very well ',
'Running' => 'not good ');
While (list ($ key, $ value) = each ($ sports )){
Echo $ key. ":". $ value ."
";
?>

Related Article

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.