a key
array() creates an array.
array_change_key_case() changes all keys in the array to lowercase or uppercase.
array_chunk() splits an array into new array blocks.
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
//Array ( [0] => Array ( [0] => Volvo [1] => BMW ) [1] => Array ( [0] => Toyota [1] => Honda ) [2] => Array ( [0] => Mercedes [1] => Opel ) )
array_column() returns the value of a single column in the input array.
array_column(array,column_key,index_key);
$last_names = array_column($a, 'last_name');
Array
(
[0] => Gates [1] => Jobs [2] => Zuckerberg)
$last_names = array_column($a, 'last_name', 'id');
Array
(
[5698] => Gates [4767] => Jobs [3809] => Zuckerberg)
array_combine() creates a new array by combining two arrays.
$fname=array("Bill","Steve","Mark");
$age=array("60","56","31");
$c=array_combine($fname,$age);
// Array ( [Bill] => 60 [Steve] => 56 [Mark] => 31 )
array_count_values() is used to count the number of occurrences of all values in the array.
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
//Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
array_diff() compares arrays and returns the difference set (only key values are compared).
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
// Array ( [d] => yellow )
array_diff_assoc() compares arrays and returns a difference set (comparing key names and key values).
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");
$result=array_diff_assoc($a1,$a2);
//Array ( [d] => yellow )
array_diff_key() compares arrays and returns difference sets (only key names are compared).
array_diff_uassoc() compares the array and returns the difference set (compare the key name and key value, and use the user-defined key name comparison function).
array_diff_ukey() compares the array and returns the difference set (only the key names are compared, and the user-defined key name comparison function is used).
array_fill() fills the array with the given key value.
php array_fill The function fills an array with key values. Its syntax is array_fill(index,number,value),
parameter index Required, the first index of the returned array;
number Required, specifies the number of elements to be inserted;
value Required, specifies the value used to populate the array.
$a1=array_fill(3,4,"blue");
// Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
array_fill_keys() fills the array with the given key value of the specified key name.
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
//Array ( [a] => blue [b] => blue [c] => blue [d] => blue )
array_filter() uses the callback function to filter the elements in the array.
$arr = ['a','b',1,2,3]; $new_arr = array_filter($arr,function($val){ return is_numeric($val); }); var_dump($new_arr);
//Return result / / array (size = 3) / / 2 = > int 1 / / 3 = > int 2 / / 4 = > int 3
array_flip() swaps keys and values in an array.
array_intersect() compares arrays and returns intersections (only key values are compared).
array_intersect_assoc() compares arrays and returns an intersection (comparing key names and key values).
array_intersect_key() compares arrays and returns the intersection (only key names are compared).
array_intersect_uassoc() compares arrays and returns the intersection (compare key names and key values, and use the user-defined key name comparison function).
array_intersect_ukey() compares arrays and returns the intersection (only key names are compared, and the user-defined key name comparison function is used).
array_key_exists() checks whether the specified key name exists in the array.
array_keys() returns all the key names in the array.
array_map() sends each value in the array to the user-defined function and returns a new value.
$arr1 = [1,2,3,4,5];
$arr2 = [6,7,8,9,10]; //The function is written before the array parameter
$new_arr = array_map(function($val1,$val2){ return $val1 + $val2; },$arr1,$arr2); var_dump($new_arr);
//Return result / / array (size = 5) / / 0 = > int 7 / / 1 = > int 9 / / 2 = > int 11 / / 3 = > int 13 / / 4 = > int 15
array_merge() combines one or more arrays into one array.
array_merge_recursive() recursively merges one or more arrays.
array_multisort() sorts multiple arrays or multidimensional arrays.
array_pad() fills the array to the specified length with a value.
array_pop() deletes the last element of the array (out of the stack).
array_product() multiplies all the values in the array.
array_push() inserts one or more elements at the end of the array (on the stack).
array_rand() returns one or more random keys in an array.
array_reduce() returns an array as a string by using user-defined functions.
array_replace() replaces the value of the first array with the value of the following array.
array_replace_recursive() recursively replaces the value of the first array with the value of the following array.
array_reverse() returns the array in reverse order.
array_search() searches for the given value in the array and returns the key name.
array_shift() deletes the first element in the array and returns the value of the deleted element.
array_unshift() inserts one or more elements at the beginning of the array.
array_slice() returns the selected part of the array.
array_splice() deletes and replaces the elements specified in the array.
array_sum() returns the sum of the values in the array.
array_udiff() compares arrays and returns difference sets (only compare values, using a user-defined key name comparison function).
array_udiff_assoc() compares arrays and returns difference sets (compare keys and values, use built-in functions to compare key names, and use user-defined functions to compare key values).
array_udiff_uassoc() compares arrays and returns the difference set (comparing keys and values, using two user-defined key name comparison functions).
array_uintersect() compares arrays and returns the intersection (only compare values and use a user-defined key name comparison function).
array_uintersect_assoc() compares arrays and returns the intersection (compare keys and values, use built-in functions to compare key names, and use user-defined functions to compare key values).
array_uintersect_uassoc() compares arrays and returns the intersection (comparing keys and values, using two user-defined key name comparison functions).
array_unique() removes duplicate values from the array.
array_values() returns all the values in the array.
array_walk() applies a user function to each member of the array.
array_walk_recursive() recursively applies user functions to each member of the array.
sort() sorts the array.
This function will assign a new key name to the cells in the sorted array, which will delete the original key name instead of reordering.
$arr = array("b", "a", "c");
sort($arr);
// Array ( [0] => a [1] => b [2] => c )
rsort() sorts the array in reverse. This will delete the original key names and not just reorder them
asort() sorts associative arrays in ascending order by key values. The key will not be changed
// Array ( [1] => a [0] => b [2] => c )
arsort() sorts associative arrays in descending order by key values.
// Array ( [a] => 20 [b] => 18 [c] => 25 )
ksort() sorts the array by key name.
krsort() sorts the array in reverse order by key name.
uasort() uses a user-defined comparison function to sort the key values in the array.
uksort() sorts the key names in the array using a user-defined comparison function.
usort() sorts the array using a user-defined comparison function.
compact() creates an array containing variable names and their values.
count() returns the number of elements in the array.
current() returns the current element in the array.
current() Function returns the value of the current element in the array.
Each array has an internal pointer to its"current"Element, initially pointing to the first element inserted into the array.
Tip: this function does not move the pointer inside the array. To do this, use next() and prev() Function.
Relevant methods:
$people = array("Bill", "Steve", "Mark", "David");
//The first is executed by default
// Bill
each() returns the current key / value pair in the array.
$people = array("Bill", "Steve", "Mark", "David");
//Array ( [1] => Bill [value] => Bill [0] => 0 [key] => 0 )
end() points the internal pointer of the array to the last element.
extract() imports variables from the array into the current symbol table.
in_array() checks whether the specified value exists in the array.
key() gets the key name from the associative array.
$people = array("Bill", "Steve", "Mark", "David");
echo "The current position of the key is:" . key($people);
//The current position of the key is: 0
list() assigns the values in the array to some variables.
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
natcasesort() uses the "natural sort" algorithm to sort arrays case insensitive.
natsort() sorts arrays using the "natural sort" algorithm.
next() moves the internal pointer in the array one bit forward.
Alias of pos() current().
prev() rewinds the internal pointer of the array by one bit.
range() creates an array containing the specified range cells.
$number = range(0,5);
print_r ($number);
// Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
reset() resets the internal pointer of the array to the first element in the array
shuffle() scrambles the array.
Alias for sizeof() count().
whole
array() Create an array.
array_change_key_case() Change all keys in the array to lowercase or uppercase.
array_chunk() Split an array into new array blocks.
array_column() Returns the value of a single column in the input array.
array_combine() Create a new array by merging two arrays.
array_count_values() Used to count the number of occurrences of all values in the array.
array_diff() Compare arrays and return difference sets (only compare key values).
array_diff_assoc() Compare arrays and return difference sets (compare key names and key values).
array_diff_key() Compare arrays and return difference sets (only compare key names).
array_diff_uassoc() Compare arrays and return difference sets (compare key names and key values, and use user-defined key name comparison functions).
array_diff_ukey() Compare arrays and return difference sets (only compare key names and use user-defined key name comparison functions).
array_fill() Fills the array with the given key value.
array_fill_keys() Fills the array with the given key value for the specified key name.
array_filter() Filter the elements in the array with the callback function.
array_flip() Swap keys and values in an array.
array_intersect() Compare arrays and return intersections (only compare key values).
array_intersect_assoc() Compare arrays and return intersections (compare key names and key values).
array_intersect_key() Compare arrays and return intersections (only compare key names).
array_intersect_uassoc() Compare arrays and return intersection (compare key names and key values, and use user-defined key name comparison function).
array_intersect_ukey() Compare arrays and return intersection (only compare key names and use user-defined key name comparison function).
array_key_exists() Checks whether the specified key name exists in the array.
array_keys() Returns all key names in the array.
array_map() Send each value in the array to the user-defined function and return a new value.
array_merge() Combine one or more arrays into one array.
array_merge_recursive() Merges one or more arrays recursively.
array_multisort() Sort multiple arrays or multidimensional arrays.
array_pad() Fills the array to the specified length with values.
array_pop() Delete the last element of the array (out of stack).
array_product() Calculates the product of all values in the array.
array_push() Inserts one or more elements at the end of the array (on the stack).
array_rand() Returns one or more random keys in an array.
array_reduce() Returns an array as a string by using user-defined functions.
array_replace() Replace the value of the first array with the value of the following array.
array_replace_recursive() Recursively replace the value of the first array with the value of the following array.
array_reverse() Returns an array in reverse order.
array_search() Searches for the given value in the array and returns the key name.
array_shift() Deletes the first element in the array and returns the value of the deleted element.
array_slice() Returns the selected part of an array.
array_splice() Deletes and replaces the elements specified in the array.
array_sum() Returns the sum of the values in an array.
array_udiff() Compare arrays and return difference sets (only compare values, using a user-defined key name comparison function).
array_udiff_assoc() Compare arrays and return difference sets (compare keys and values, use built-in functions to compare key names, and use user-defined functions to compare key values).
array_udiff_uassoc() Compare arrays and return difference sets (compare keys and values, and use two user-defined key name comparison functions).
array_uintersect() Compare arrays and return intersection (only compare values, using a user-defined key name comparison function).
array_uintersect_assoc() Compare arrays and return intersection (compare keys and values, use built-in functions to compare key names, and use user-defined functions to compare key values).
array_uintersect_uassoc() Compare arrays and return intersection (compare keys and values, and use two user-defined key name comparison functions).
array_unique() Remove duplicate values from the array.
array_unshift() Insert one or more elements at the beginning of the array.
array_values() Returns all values in the array.
array_walk() Apply user functions to each member of the array.
array_walk_recursive() Apply user functions recursively to each member of the array.
arsort() Sort associative arrays in descending order by key values.
asort() Sort associative arrays in ascending order by key values.
compact() Create an array containing variable names and their values.
count() Returns the number of elements in an array.
current() Returns the current element in the array.
each() Returns the current key in the array/Value pair.
end() Point the internal pointer of the array to the last element.
extract() Imports variables from the array into the current symbol table.
in_array() Checks whether the specified value exists in the array.
key() Gets the key name from the associative array.
krsort() Sort the array in reverse order by key name.
ksort() Sort the array by key name.
list() Assign the values in the array to some variables.
natcasesort() Use the "natural sorting" algorithm to sort the array case insensitive.
natsort() Use the "natural sorting" algorithm to sort the array.
next() Moves the internal pointer in the array one bit forward.
pos() current() Alias for.
prev() Rewind the internal pointer of the array by one bit.
range() Creates an array containing cells of the specified range.
reset() Point the internal pointer of the array to the first element.
rsort() Reverse sort the array.
shuffle() Scramble the array.
sizeof() count() Alias for.
sort() Sort the array.
uasort() Use the user-defined comparison function to sort the key values in the array.
uksort() Sort the key names in the array using a user-defined comparison function.
usort() Sort the array using a user-defined comparison function.