PHP--PHP array-related functions

Source: Internet
Author: User

Array range (mixed $low, mixed $high [, number $step])

To create a contiguous array

Range (' A ', ' Z '); foreach (range as$number) {echo$number;} // Array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one, one)

BOOL Shuffle (array & $array)

Random Sorting of arrays

<? PHP $numbers Range (1); Shuffle ($numbers); foreach ($numbers as$number) {echo "$number ";}? >

Array each (array & $array)

Returns the array element information pointed to by the current array pointer, which contains 4 elements, where the value of the key that is labeled 0 and key corresponds to the key of the array element corresponding to the pointer, and the value of 1 and value corresponds to the value of the array element that the current pointer points to.

Moves the array pointer down one bit at the same time. You can use the Reset () function to reset its pointer

<? PHP $fruit Array (' a ' + = ' apple ', ' b ' = ' banana ', ' c ' = ' cranberry '); Reset ($fruit);  while (list($key$valeach ($fruit)) {echo ] $key $val\ n ";}? >

Mixed current (array & $array)

Returns the element that the current array pointer refers to, but does not move the array pointer. Returns False if the array is an empty array or if the array pointer exceeds the last element in the array. (Note that it is false to be careful, because if the pointer is in the element

A value of 0 or false may also result in a return of false, which can be determined using "= = =").

Mixed prev (array & $array)

Moves the array pointer forward one position, and the method returns False when the pointer cannot move forward.

Mixed Next (array & $array)

Moves the array pointer backward one bit and returns its element value.

Mixed End (array & $array)

Moves the array pointer to the last element in the array and returns its element value, False if the array is empty.

Mixed Reset (array & $array)

Resets the element pointer to the position of the first element and returns False if the element is empty.

Mixed key (array & $array)

Returns the key of the element that contains the current array pointer, or null if an empty array or pointer points to the element following the largest element of the array.

BOOL Sort (array & $array [, int $sort _flags = sort_regular])

Sorts the elements in the array from small to large (the sort is case-sensitive, and uppercase letters are preceded by lowercase letters.) and the function will delete the original key name and not just reorder it.

$sort _flags Parameters:

Sort_regular Normal format sort, default sort mode

Sort_numeric to sort elements by numbers

Sort_string to sort elements by string

The sort succeeds returns True, otherwise returns false

BOOL Asort (array &array [, int sort_flags])

Sorts the values of an array element in ascending order and keeps the key associated. Returns TRUE if successful, and FALSE if it fails. Sort_flags is the same as the sort function

BOOL Arsort (array &array [, int sort_flags])

Sorts the values of an array element in descending order and keeps the key associated. Returns TRUE if successful, and FALSE if it fails. Sort_flags is the same as the sort function

BOOL Ksort (array &array [, int sort_flags])

Sorts the array by key name in ascending order and preserves corresponding key values. Returns TRUE if successful, and FALSE if it fails. Sort_flags is the same as the sort function

BOOL Krsort (array &array [, int sort_flags])

Sorts the array by key name in descending order and preserves corresponding key-value correspondence. Returns TRUE if successful, and FALSE if it fails. Sort_flags is the same as the sort function

BOOL Usort (array & $array, callback $cmp _function)

An array of user-defined function cmp_function to sort. The comparison function must return an integer less than, equal to, or greater than 0, respectively, when the first argument is considered less than, equal to, or greater than the second argument.

This function assigns a new key name to the cells in the array. This will delete the original key name and not just reorder it.

BOOL Uasort (array &array, callback cmp_function)

The Uasort () function uses a user-defined comparison function to sort the array and keep the index associated (no new keys are assigned to the element).

Returns TRUE if successful, otherwise FALSE.

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

<?PHPfunctionMy_sort ($a,$b)  {  if($a==$b)return0; return($a>$b) ? -1:1; }$people=Array("Swanson" and "Joe", "Griffin" and "Peter", "Quagmire" and "Glenn", "Swanson" = "Joe", "Griffin" and "Peter", " Quagmire "=" Glenn "); Uasort($people, "My_sort");Print_r($people);?>

Results:

Array ([Griffin] = Peter[swanson] [Joe[quagmire] = Glenn[griffin] = Peter[swanson] = Joe[quagmire] =& Gt Glenn)

BOOL Uksort (array &array, callback cmp_function)

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.

<?PHPfunctionMy_sort ($a,$b)  {  if($a==$b)return0; return($a>$b) ? -1:1; }$people=Array("Swanson" and "Joe", "Griffin" and "Peter", "Quagmire" and "Glenn", "Swanson" = "Joe", "Griffin" and "Peter", " Quagmire "=" Glenn "); Uksort($people, "My_sort");Print_r($people);?>

Results:

Array ([Swanson] = Joe[quagmire] [Glenn[griffin] = Peter[swanson] = Joe[quagmire] = Glenn[griffin] =& Gt Peter)
 

Array array_reverse (array $array [, bool $preserve _keys = false])

Reverse sorts the array, preserving the original key name if $preserve_keys is ture.

int Array_push (array &array, mixed var [, mixed ...])

Pushes one or more elements to the end of the array, and the array length changes accordingly. Note: If you use Array_push () to add a cell to an array, you might as well use $array [] =, because there is no additional burden of calling the function. The function returns the

Back to a new function after adding an element.

Mixed Array_pop (array & $array)

Deletes the last element of an array element and returns the element, or null if the element is empty.

Mixed Array_shift (array &array)

Moves the first cell of the array out and returns as a result, reducing the length of the arrays and moving all other cells forward one bit. All numeric key names are changed from zero to start, and the text key name is unchanged. If the array is empty (or not an array),

Then NULL is returned.

int Array_unshift (array &array, mixed var [, mixed ...])

Inserts the incoming cell at the beginning of the array. Note that the units are inserted as a whole, so incoming cells will remain in the same order. All numeric key names are modified to count back from zero, and all text key names remain unchanged.

BOOL Array_walk (array & $array, callback $funcname [, mixed $userdata])

Invokes a user-defined function for each element in an array. Typically, the funcname accepts two parameters. The value of the array parameter as the first, the key name as the second. If an optional parameter userdata is provided, it is passed as the third argument to the

Callback FuncName.

Note: If funcname needs to directly act on the values in the array, the first argument to FuncName is specified as a reference. Any changes to these cells will also change the original array itself.

BOOL Array_walk_recursive (array &input, callback funcname [, mixed UserData])

Applies the user-defined function funcname to each cell in an array of array. This function is recursive to a deeper array. Typically, the funcname accepts two parameters. The value of the input parameter as the first, the key name as the second. Such as

The UserData is provided with an optional parameter, which is passed as the third parameter to the callback funcname. Returns TRUE if successful, FALSE if unsuccessful

Array Array_map (callback callback, array arr1 [, array ...])

Returns an array that contains all the cells in the ARR1 after the callback action. The number of arguments callback accepts should be the same as the number of arrays passed to the Array_map () function.

int count (mixed var [, int mode]) or alias sizeof ()

Computes the number of array elements. If the optional mode parameter is set to Count_recursive (or 1), COUNT () counts recursively on the array. is especially useful for calculating all cells of a multidimensional array. The default value for mode is 0. COUNT () Recognition

No infinite recursion.

Array array_count_values (array input)

Returns an array that uses the value in the input array as the key name, the number of occurrences in the input array as the value.

Array array_values (array input)

Returns all the values in the input array and establishes a numeric index on them.

Array Array_keys (array input [, mixed search_value [, BOOL strict]])

Returns the key name of a number or string in the input array.

BOOL Array_key_exists (mixed key, array search)

Array_key_exists () returns TRUE when the given key is present in the array. Key can be any value that can be indexed as an array. Array_key_exists () can also be used for objects.

BOOL In_array (mixed needle, array haystack [, BOOL strict])

Searches for needle in haystack and returns TRUE if found, otherwise FALSE.

If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack. If needle is a string, the comparison is case-sensitive. Before PHP version 4.2.0,

Needle is not allowed to be an array.

Mixed Array_search (mixed needle, array haystack [, BOOL strict])

Returns FALSE if the needle parameter is searched in haystack and the key name is returned if found.

If the optional third parameter, strict, is TRUE, Array_search () also checks the type of needle in haystack.

If needle appears more than once in haystack, the first matching key is returned. To return all keys that match a value, you should use Array_keys () plus optional parameter search_value instead.

int extract (array var_array [, int extract_type [, string prefix]])

This function is used to import variables from the array into the current symbol table. Accept the associative array var_array as the argument and use the key name as the variable name, and the value as the value of the variable. For each key/value pair, the variables are created in the current symbol table and are

The effects of extract_type and prefix parameters.

Array compact (mixed varname [, mixed ...])

The compact () accepts a variable number of parameters. Each parameter can be a string that includes the variable name or an array containing the variable name, which can also contain an array of other cell contents as variable names, and the compact () can be recursively processed.

For each parameter, the compact () finds the variable name in the current symbol table and adds it to the output array, where the variable name becomes the key name and the contents of the variable become the value of the key. Simply put, what it does is just the opposite of extract (). Returns all the variables

The array to add in.

Array array_chunk (array input, int size [, bool Preserve_keys])

Splits an array into multiple arrays, where the number of cells per array is determined by size. The number of cells in the last array may be few. The resulting array is a cell in a multidimensional array whose index starts at zero.

Setting the optional parameter Preserve_keys to TRUE allows PHP to keep the original key name in the input array. If you specify FALSE, then each result array will be indexed with a new zero-based number. The default value is FALSE.

Array array_combine (array keys, array values)

Returns an array, using the value from the keys array as the key name, from the values of the value array as the corresponding value.

Returns FALSE if the number of cells in the two array is different or if the array is empty.

Array array_filter (array input [, callback callback])

Array_filter () passes each value in the input array sequentially to the callback function. If the callback function returns TRUE, the current value of the input array is included in the returned result array. The key name of the array remains unchanged.

Array_merge ()

Merges the cells of one or more arrays and appends the values in an array to the previous array. Returns an array as the result.

If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.

If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.

PHP--PHP array-related functions

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.