Php Array Operations: common PHP array related functions

Source: Internet
Author: User
During programming, I often forget the usage of some Arrays. I only remember that there is such a function. I don't know how to use it. now I use it as a manual.

During programming, I often forget the usage of some Arrays. I only remember that there is such a function. I don't know how to use it. now I am using it as a manual ..

Range ($ low, $ high), range ($ low, $ high, $ step); // create an array of sequential values, for example, range () is) another example is range ('A', 'z ')

Each ($ array) returns the current element of the array in sequence, and sets the next element as the current element;

Reset ($ array) to reset the current element of the array to the beginning of the array

List () can be used to break an array into a series of values, such as list ($ a, $ B) = each ($ array)

Shuffle ($ array), array_rand ($ arg, $ num_req); random sorting of arrays

Array_reverse ($ input), array_reverse ($ input, $ preserve_keys) returns the reverse sorting of the original array

Sort ($ array); sorts arrays

 

PHP array is an important concept. it contains a large number of functions for people's development... Now we classify its arrays to facilitate query and application.
Let's talk about the definition of PHP array... The PHP array contains two items: key and value. you can use the key to obtain the corresponding value. The key can be a value and associated, for example, $ array [0]. $ array [one]…
Create an array
The array declaration in PHP is a little different from that in other languages, but it can also be declared as one-dimensional, two-dimensional, three-dimensional and multi-dimensional, such
$ Array [0] = 1, $ array = array (, 3); one-dimensional array, which contains only three values and belongs to a numeric array, $ array [0] can be used to represent 1 during Reference. indexes can be omitted when a numeric array is created.
$ Array = array (
1 => "one ",
2 => "two ",
3 => "three ",
4 => array (
"One" => 1,
"Two" => 2,
"Three" => 3
)
);
A two-dimensional array is also an associated array. you can use $ array [4] ["one"] to represent 1.
3D or above...
If you want to create arrays in batches, you can use the following function:
Array range (mixed low, mixed high [, number step])
For example, $ array = range (); indicates array );
$ Array = range (a, f); represents array (a, B, c, d, e, f );

Output array
PHP outputs many array functions, commonly used
Bool print_r (mixed expression [, bool return])
Void var_dump (mixed expression [, mixed expression [,...])
Also, echo, print, and printf can output a single array.

Test array
Sometimes we need to determine whether a variable is an array, we can use:
Bool is_array (mixed var)

Add or delete array elements
After the array is declared, it is not static. you may perform in-depth operations by adding and deleting arrays:
Int array_push (array & array, mixed var [, mixed...]) press one or more units to the end of the array. 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 up the last element of the array (Out Stack), and resets the array 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 units at the beginning of the array
Array array_pad (array input, int pad_size, mixed pad_value) fill the array with a value to the specified length, such as array_pad ($ array, 3, $ var );

Locate array elements
Bool in_array (mixed needle, array haystack [, bool strict]) checks whether 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 reassembles them into a new array.
Bool array_key_exists (mixed key, array search) checks whether the given key exists in the array.
Array array_values (array input) returns all values in the array.
Mixed array_search (mixed needle, array haystack [, bool strict]) searches for a given value in the array. if the value is successful, the key is returned.

Traverse arrays
PHP provides many functions for getting keys and values.
Mixed key (array & array) obtains the key name from the associated array
Mixed reset (array & array) resets the array pointer
Array each (array & array) returns the key/value pairs in the array and moves the array one step forward.
Mixed current (array & array) returns the current unit in the array
Mixed end (array & array) moves the pointer in the array to the last
Mixed next (array & array) move the pointer in the array to the next
Mixed prev (array & array) moves the pointer in the array to the previous position
Array array_reverse (array [, bool preserve_keys]) returns an array with the opposite unit order.
Array array_flip (array trans) swap the key-value roles in the array
In addition to the above functions, you can also use loops to traverse elements in the array, as shown in
Foreach (array_expr as $ value)
{Statement}
Foreach (array_expr as $ key => $ value)
{Statement}
Extract each key/value pair until all items are obtained or some internal conditions are met.
Void list (mixed varname, mixed...) assigns values in the array to some variables

Determine array size and uniqueness
Int count (mixed var [, int mode]) calculates the number of unit arrays or attributes of objects in the array. sizeof has the same name.
Array array_count_values (array input) counts the number of occurrences of all values in the array
Array array_unique (array) removes repeated values from the array.

Array sorting
I heard this is the core issue of the calculator... Haha... The same is true...
Bool sort (array & array [, int sort_flags]) sorts arrays.
Bool natsort (array & array) sorts arrays by natural sorting
Bool natcasesort (array & array) sorts arrays by natural sorting, case insensitive
Bool rsort (array & array [, int sort_flags]) sorts arrays in reverse order.
Bool asort (array & array [, int sort_flags]) sorts arrays and maintains the index relationship.
Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...]) sorts multiple arrays or multi-dimensional arrays
Bool arsort (array & array [, int sort_flags]) sorts arrays in reverse order and maintains the index relationship.
Bool ksort (array & array [, int sort_flags]) sorts the array key names
Bool krsort (array & array [, int sort_flags]) sorts the array key names in reverse order.

Merge, split, combine, and split arrays
Array array_combine (array keys, array values) creates an array. The value of one array is used as its key name, and the value of the other array is used 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, int offset [, int length [, bool preserve_keys]) extracts a segment from the array and creates a new array. if offset is a positive number, split starts from the offset position of the array switch. if it is a negative number, the split starts from the offset position at the end of the array. at this time, the split ends from the count (input_array)-| length | position of the array switch.
Array array_splice (array & input, int offset [, int length [, array replacement]) removes the partial value from the array and replaces it with other values. the offset setting is the same as above.
Array array_intersect (array array1, array array2 [, array...]) calculates the intersection of arrays, that is, if the values in the first array appear in the following arrays, this value is taken out.
Array array_intersect_assoc (array array1, array array2 [, array...]) contains the intersection in the index check array.
Array array_intersect_key (array array1, array array2 [, array ...])
Array array_diff (array array1, array array2 [, array...]) calculates the difference set of the array, that is, different values from the first array.
Array array_diff_assoc (array array1, array array2 [, array...]) contains the difference set in the index check array.
Array array_diff_key (array array1, array array2 [, array...]) use the key name to compare the difference sets in the array.

Other useful array functions
A lot of array functions are not listed... A few more useful ones are also quite common. for other ones, refer to the manual... The manual is clear.
Mixed array_rand (array input [, int num_req]) one or more keys are randomly taken out of the array. num indicates the number of keys.
Bool shuffle (array & array) disrupts the array
Number array_sum (array) calculates the sum of all values in the array. the associated array is ignored.
Array array_chunk (array input, int size [, bool preserve_keys]) splits an array into several


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.