PHP array functions (6)

Source: Internet
Author: User
Tags bitwise compact

The 1.arsort () function sorts the associative array in descending order of key values

Arsort (array,sortingtype);

    • 0 = sort_regular-default. Arrange each item in the usual order (standard ASCII, no change type)
    • 1 = sort_numeric-Each item is treated as a number.
    • 2 = sort_string-Each item is treated as a string.
    • 3 = sort_locale_string-handles each item as a string, based on the current locale (can be changed by setlocale ()).
    • 4 = sort_natural-handles each item as a string, using a natural sort similar to Natsort ().
    • 5 = sort_flag_case-You can sort strings by combining (bitwise OR) sort_string or sort_natural, not case-sensitive.

The Arsort () function reverses the array and maintains the index relationship. It is primarily used to sort the associative arrays that are important for the cell order.

The Second optional parameter contains an additional sort ID.

Returns TRUE if successful, otherwise FALSE.

2. Asort () function to sort the associative array in descending order of key values

Asort (Array,sortingtype);

The Asort () function sorts the array and maintains the index relationship. It is primarily used to sort the associative arrays that are important for the cell order.

The Second optional parameter contains an additional sort ID.

Returns TRUE if successful, otherwise FALSE.

3. The compact () function creates an array that contains the variable names and their values

Compact (var1,var2...)

The compact () function creates an array of variables with the arguments. If an array exists in the parameter, the value of the variable in the array is also fetched.

The array returned by this function is an associative array, the key name is the parameter of the function, and the key value is the value of the variable in the parameter.

This function performs exactly the opposite of extract ().

4. The extract () function imports variables from an array into the current symbol table

Extract (array,extract_rules,prefix)
    • Extr_overwrite-Default. If there is a conflict, overwrite the existing variable.
    • Extr_skip-If there is a conflict, do not overwrite the existing variable.
    • Extr_prefix_same-If there is a conflict, precede the variable name with the prefix PREFIX.
    • Extr_prefix_all-Prefix all variable names with PREFIX.
    • Extr_prefix_invalid-Prefix PREFIXonly before the illegal or numeric variable name.
    • Extr_if_exists-Overrides the value of a variable with the same name in the current symbol table only. None of the others will deal with it.
    • Extr_prefix_if_exists-A variable name with the prefix appended to it is created only if a variable with the same name already exists in the current symbol table, and none of the others are processed.
    • Extr_refs-Extracts the variable as a reference. The imported variable still references the value of the array parameter.

prefix is optional. Please note that the value of prefix only in extract_type is extr_prefix_same,extr_prefix_all,extr_prefix_invalid or extr_prefix_ Required when if_exists. If the result appended with the prefix is not a valid variable name, it will not be imported into the symbol table.

An underscore is automatically added between the prefix and the array key name.

The function uses the array key name as the variable name, using the array key value as the variable value. For each element in the array, a corresponding variable is created in the current symbol table.

The second parameter type is used to specify how the extract () function treats conflicts when a variable already exists and the element with the same name in the array.

The function returns the number of variables that were successfully imported into the symbol table.

5. Count (), the sizeof () function returns the number of elements in the array

COUNT (array,mode);

Possible values for mode:

    • 0-Default. Do not count all elements in a multidimensional array
    • 1-counts the number of elements in the array recursively (computes all elements in a multidimensional array)

The count () function calculates the number of cells in an array or the number of properties in an object.

For an array, returns the number of its elements, and returns 1 for other values. Returns 0 if the argument is a variable and the variable is not defined.

If mode is set to Count_recursive (or 1), the number of elements in the array in the multidimensional array is recursively computed.

6. Current (), the POS () function returns the value of the present element in the array

Each array has an internal pointer to its "current" element, which initially points to the first element inserted into the array.

The current () function returns the value of the array element that is currently pointed to by the internal pointer and does not move the pointer. If the internal pointer points beyond the end of the cell list, current () returns FALSE.

Related methods:

    • End ()-Points the inner pointer to the last element in the array and outputs
    • Next ()-Points the inner pointer to the next element in the array and outputs
    • Prev ()-Points the internal pointer to the previous element in the array and outputs
    • Reset ()-Points the inner pointer to the first element in the array and outputs
    • each ()-Returns the key name and key value of the current element and moves the internal pointer forward

7. The In_array () function searches the array for the presence of the specified value

In_array (search,array,type)

Type is optional. If set to true, checks whether the searched data is the same as the type of the array's value.

8. The key () function returns the key name of the array inside the pointer currently pointing to the element

Key (array)

9. Krsort () function to sort the associative array in descending order by key name

Krsort (array,sortingtype);

Sortingtype

Specifies how to arrange the elements/items of an array. Possible values:

    • 0 = sort_regular-default. Arrange each item in the usual order (standard ASCII, no change type)
    • 1 = sort_numeric-Each item is treated as a number.
    • 2 = sort_string-Each item is treated as a string.
    • 3 = sort_locale_string-handles each item as a string, based on the current locale (can be changed by setlocale ()).
    • 4 = sort_natural-handles each item as a string, using a natural sort similar to Natsort ().
    • 5 = sort_flag_case-You can sort strings by combining (bitwise OR) sort_string or sort_natural, not case-sensitive.

The Ksort () function sorts the associative array in ascending order by key name

One. The list () function is used to assign values to a set of variables in a single operation

List (var1,var2...)

The 12.natcasesort () function sorts the array with a "natural sort" algorithm

Key values retain their original key names

In the natural sorting algorithm, the number 2 is less than the number 10. In the computer sorting algorithm, 10 is less than 2 because the first digit in "10" is less than 2.

The function is not case sensitive.

If successful, the function returns TRUE if it fails and returns FALSE.

The Natsort () function uses the "natural sort" algorithm to sort the array. Key values retain their original key names

The range () function creates an array of elements that contain the specified range

The Rsort () function sorts a numeric array in descending order

The shuffle () function rearranges the elements in the array in random order

The function assigns a new key name to the elements in the array. An existing key name is deleted

The Uasort () function uses a user-defined comparison function to sort the array and keep the index association (no new key is assigned to the element)

Uasort (array,myfunction);

Returns TRUE if successful, otherwise FALSE.

This function is primarily used to sort the associative arrays that are important for the cell order.

The Uksort () function sorts the group key names by a user-defined comparison function

Uksort (array,myfunction);

The Uksort () function uses a user-defined comparison function to sort the array by key name and maintain the index relationship.

Returns TRUE if successful, otherwise FALSE.

This function should be used if the array to be sorted needs to be sorted with an unusual standard.

The custom function should accept two parameters, which will be populated with a pair of key names in the array. The comparison function must return an integer less than 0, equal to zero, or greater than 0 when the first parameter is less than, equal to, or greater than the second argument.

Usort () Sorting arrays by user-defined comparison function

Usort (array,myfunction);

If two elements compare the same result, their order in the sorted array is undefined. Before PHP 4.0.6, the user-defined function retains the original order of these elements. However, because of the introduction of a new sorting algorithm in 4.1.0, the result will not be this, because there is no effective solution to this.

This function assigns the new key name to the element in the array . This will delete the original key name.

PHP array functions (6)

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.