PHP array functions (4)

Source: Internet
Author: User
Tags sorts

1. The Array_merge () function merges one or more arrays into an array

Array_merge (array1,array2,array3...)

If two or more array elements have the same key name, the last element overrides the other element.

If you enter an array only to the Array_merge () function, and the key name is an integer, the function returns a new array with an integer key name whose key name is re-indexed starting at 0.

2. The array_merge_recursive () function merges one or more arrays into an array

Array_merge_recursive (array1,array2,array3...)

The function differs from the Array_merge () function when processing two or more array elements with the same key name. Array_merge_recursive () does not overwrite the key name, but instead recursively composes the values of multiple identical key names into an array.

If you only enter an array to the array_merge_recursive () function, and the result is the same as Array_merge (), the function returns a new array with an integer key name, whose key name is re-indexed starting at 0.

3. The Array_multisort () function returns a sorted array. You can enter one or more arrays. The function first sorts the first array, then the other arrays, and if two or more values are the same, it sorts the next array.

Array_multisort (array1,sorting order,sorting type,array2,array3 ...)

sorting order is optional. Specify the order of arrangement. Possible values:

    • SORT_ASC-Default. Sorted in ascending order (A-Z).
    • Sort_desc-Sorted in descending order (Z-A).

Sorting type

Optional. Specifies the sort type. Possible values:

    • Sort_regular-Default. Arrange each item in the usual order (standard ASCII, without changing the type).
    • Sort_numeric-Handle each item as a number.
    • Sort_string-handles each item as a string.
    • Sort_locale_string-handles each item as a string, based on the current locale (can be changed by setlocale ()).
    • Sort_natural-handles each item as a string, using a natural sort similar to Natsort ().
    • Sort_flag_case-You can sort strings by combining (bitwise OR) sort_string or sort_natural, not case-sensitive.

The string key name is preserved, but the numeric key name is re-indexed, starting at 0 and incrementing by 1.

4. The Array_pad () function inserts the specified number of elements with the specified value into the array

Array_pad (array,size,value)

If you set the size parameter to a negative number, the function inserts a new element before the original array (see the following instance).

Note: If the size parameter is less than the length of the original array, the function does not delete any elements.

Returns an array with the new elements.

5. The Array_pop () function deletes the last element in the array

Array_pop (array)

Returns the last value of the array. If the array is empty, or is not an array, NULL is returned.

6. The array_product () function computes and returns the product of the array

Returns the product of an integer or floating-point number.

From PHP 5.3.6, the product of an empty array is 1. Before PHP 5.3.6, the product of an empty array was 0.

7. The Array_push () function adds one or more elements (into the stack) to the end of the array of the first parameter, and then returns the length of the new array

The function equals multiple calls $array [] = $value.

Array_push (array,value1,value2...)

Even if there is a string key in the array, the element you add is always a numeric key. (see Example 2)

If you use Array_push () to add a unit to an array, you might as well use $array [] =, because there is no additional burden on calling the function.

8. The Array_rand () function returns the random key name in the array, or returns an array containing the random key name if you specify that the function returns more than one key name

Array_rand (array,number) is optional. Specifies how many random key names to return.

9. The Array_reduce () function sends a value from the array to the user-defined function and returns a string

If the array is empty and the initial parameter is not passed, the function returns NULL

Array_reduce (array,myfunction,initial)

The Array_reduce () function iteratively simplifies the array to a single value using a callback function.

If you specify a third parameter, the parameter is treated as the first value in the array, or the final return value if the array is empty.

The Array_replace () function replaces the value of the first array with the value of the following array

You can pass an array, or multiple arrays, to a function.

Array_replace (array1,array2,array3...)

If a key exists in the first array array1 also exists in the second array array2, the value in the first array array1 will be the second array array2 Replace the value in the. If a key exists only in the first array array1, it will remain unchanged.

If a key exists in the second array array2, but does not exist in the first array array1, the element is created in the first array array1 .

If more than one substitution array is passed, they are processed sequentially, and the values of the arrays are overwritten by the value of the previous array.

The. Array_replace_recursive () function recursively replaces the value of the first array with the value of the subsequent array

You can pass an array, or multiple arrays, to a function.

Array_replace_recursive (array1,array2,array3...)

If a key exists in the first array array1 also exists in the second array array2, the value in the first array array1 will be replaced by the value in the second array array2 . If a key exists only in the first array array1, it will remain unchanged. If a key exists in the second array array2, but does not exist in the first array array1, the element is created in the first array array1 . If more than one substitution array is passed, they are processed sequentially, and the values of the arrays are overwritten by the value of the previous array.

Returns the replaced array, or NULL if an error occurs.

PHP array functions (4)

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.