Common PHP array Functions

Source: Internet
Author: User

Array range (mixed $ low, mixed $ high [, number $ step])
Create a continuous Array
Range ('A', 'z ');
Foreach (range (0, 12) as $ number ){
Echo $ number;
}
// Array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

Bool shuffle (array & $ array)
Random sorting of Arrays
<? PHP
$ Numbers = range (1, 20 );
Shuffle ($ numbers );
Foreach ($ numbers as $ number ){
Echo "$ number ";
}
?>

Array each (array & $ array)
Returns the information of an array element pointed to by the current array pointer. The returned array contains four elements. The subscript is 0 and the key value corresponds to the key of the array element corresponding to the pointer, 1. The value corresponding to the value key is the value of the array element pointed to by the current pointer, And the array pointer is moved down by one. You can use the reset () function to reset its pointer.
<? PHP
$ Fruit = array ('A' => 'apple', 'B' => 'bana', 'c' => 'cranberry ');

Reset ($ fruit );
While (List ($ key, $ Val) = each ($ fruit )){
Echo "$ key => $ Val \ n ";
}
?>

Mixed current (array & $ array)
Returns the element indicated by the current array pointer, but does not move the array pointer. If this array is an empty array or the last element of the array whose pointer exceeds, false is returned. (Be careful when determining whether the value is false. If the value of the element where the pointer is located is 0 or false, the returned result may be false. You can use "=" to determine the value ).

Mixed Prev (array & $ array)
Move the array pointer to a forward position. If the pointer cannot move forward, this method returns false.

Mixed Next (array & $ array)
Move the array pointer one bit backward and return its element value.

Mixed end (array & $ array)
Move the array pointer to the last element in the array and return its element value. If the array is empty, false is returned.

Mixed reset (array & $ array)
Resets the element pointer to the position of the first element. If the element is null, false is returned.

Mixed key (array & $ array)
Returns the key of the element of the current array pointer. If it is an empty array or the Pointer Points to the element following the maximum element of the array, null is returned.

Bool sort (array & $ array [, int $ sort_flags = sort_regular])
Sort the elements in the array from small to large (Sorting is case-sensitive. uppercase letters are placed before lowercase letters. This function also deletes the original key name, not just the re-order.)
$ Sort_flags parameters:
Sort_regular is sorted in normal format, which is the default sorting method.
Sort_numeric sorts elements by numbers
Sort_string sorts elements by string
Returns true if sorting is successful; otherwise, returns false.

Bool asort (array & array [, int sort_flags])
Sort the values of array elements in ascending order and maintain key Association. If the call succeeds, true is returned. If the call fails, false is returned. The sort_flags and sort functions are the same.

Bool arsort (array & array [, int sort_flags])
Sort the values of array elements in descending order and maintain key Association. If the call succeeds, true is returned. If the call fails, false is returned. The sort_flags and sort functions are the same.

Bool ksort (array & array [, int sort_flags])
Sort the array in ascending order by key name, and retain the corresponding key value. If the call succeeds, true is returned. If the call fails, false is returned. The sort_flags and sort functions are the same.

Bool krsort (array & array [, int sort_flags])
Sort the array in descending order by key name, and retain the corresponding key value. If the call succeeds, true is returned. If the call fails, false is returned. The sort_flags and sort functions are the same.

Bool usort (array & $ array, callback $ cmp_function)
Sort the array by the User-Defined Function cmp_function. A comparison function must return an integer less than, equal to, or greater than zero when the first parameter is considered to be less than, equal to, or greater than the second parameter.
This function assigns a new key name to the Unit in array. This will delete the original key name, not just the reorder.

Bool uasort (array & array, callback cmp_function)

Bool uksort (array & array, callback cmp_function)

Array array_reverse (array $ array [, bool $ preserve_keys = false])
Sort the array in reverse order. If $ preserve_keys is true, the original key name is retained.

Int array_push (array & array, mixed var [, mixed...])
Push one or more elements to the end of the array, and the length of the array varies accordingly.NOTE: If array_push () is used to add a unit to the array, it is better to use $ array [] =, because there is no additional burden to call the function. This function returns a new function after an element is added.

Mixed array_pop (array & $ array)
Delete the last element of the array element and return the element. If the element is null, null is returned.

Mixed array_shift (array & array)
Removes the first element of the array and returns the result. This removes the length of the array and moves all other elements one by one. The names of all numeric keys are counted from scratch, and the names of text keys remain unchanged. If array is null (or not an array), null is returned.

Int array_unshift (array & array, mixed var [, mixed...])
Insert the input unit to the beginning of the array. Note that the units are inserted as a whole, so the incoming units are in the same order. The names of all numeric keys are changed to zero-start counting, and the names of all text keys remain unchanged.

Bool array_walk (array & $ array, callback $ funcname [, mixed $ userdata])
Call User-defined functions for each element in the array. In typical cases, funcname accepts two parameters. The value of the array parameter is the first and the key name is the second. If the optional parameter userdata is provided, it is passed to callback funcname as the third parameter.
NOTE: If funcname needs to act directly on the value in the array, specify the first parameter of funcname as a reference. In this way, any change to these units will also change the original array itself.

Bool array_walk_recursive (array & input, callback funcname [, mixed userdata])
Apply the User-Defined Function funcname to each cell in the array. This function is recursive to a deeper array. In typical cases, funcname accepts two parameters. The value of the input parameter is the first and the key name is the second. If the optional parameter userdata is provided, it is passed to callback funcname as the third parameter. If the call succeeds, true is returned. If the call fails, false is returned.

Array array_map (callback, array arr1 [, array...])
Returns an array containing all the units in arr1 that have passed the callback function. The number of parameters accepted by callback should be the same as the number of arrays passed to the array_map () function.

Int count (mixed var [, int mode]) or alias sizeof ()
Calculates the number of array elements. If the optional mode parameter is set to count_recursive (OR 1), count () recursively counts the array. It is particularly useful for calculating all units of a multi-dimensional array. The default value of mode is 0. Count () cannot recognize infinite recursion.

Array array_count_values (array input)
Returns an array that uses the value in the input array as the key name and the number of times this value appears in the input array as the value.

Array array_values (array input)
Return all values in the input array and create a digital index for 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 if the given key exists in the array. Key can be any value that can be used as an array index. Array_key_exists () can also be used for objects.

Bool in_array (mixed needle, array haystack [, bool strict])
Search for needle in haystack. If yes, true is returned. Otherwise, false is returned.
If the value of the third strict parameter is true, the in_array () function checks whether the needle type is the same as that in haystack. If the needle is a string, the comparison is case sensitive. Before PHP version 4.2.0, needle cannot be an array.

Mixed array_search (mixed needle, array haystack [, bool strict])
Search for the needle parameter in haystack and return the key name if it is found. Otherwise, false is returned.
If the optional third parameter strict is true, array_search () will also check the needle type in haystack.
If needle appears more than once in haystack, the first matched key is returned. To return keys of all matched values, replace array_keys () with the optional parameter search_value.

Int extract (array var_array [, int extract_type [, string prefix])
This function is used to import variables from the array to the current symbol table. The array var_array is used as the parameter and the key name is used as the variable name, and the value is used as the variable value. Each key/value pair will create a variable in the current symbol table and be affected by the extract_type and prefix parameters.

Array compact (mixed varname [, mixed...])
Compact () accepts variable parameter numbers. Each parameter can be a string containing the variable name or an array containing the variable name. The array can also contain an array with other units whose content is the variable name. Compact () can be recursively processed.
For each parameter, compact () searches for the variable name in the current symbol table and adds it to the output array, the variable name becomes the key name and the content of the variable becomes the key value. Simply put, it does the opposite thing as extract. Returns the array after all variables are added.

Array array_chunk (array input, int size [, bool preserve_keys])
Splits an array into multiple arrays. The number of units in each array is determined by the size. The number of units in the last array may be smaller. The obtained array is a unit in a multi-dimensional array, and its index starts from scratch.
Set the optional parameter preserve_keys to true so that PHP retains the original key name in the input array. If you specify false, each result array is indexed with a new number starting from scratch. The default value is false.

Array array_combine (array keys, array values)
Returns an array with the value from the keys array as the key name and the value from the values array as the corresponding value.
If the number of cells in two arrays is different or the array is empty, false is returned.

Array array_filter (array input [, callback])
Array_filter () transmits each value in the input array to the callback function in sequence. 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 ()
Combine the elements of one or more arrays. The values of an array are appended to the values of the previous array. Returns an array of results.
If the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.
If only one array is assigned and the array is indexed by number, the key name is re-indexed continuously.

 PHP Technology Exchange Group 170855791

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.