PHP built-in array functions
PHP has a built-in great support for the array to work with. There are numerous inbuilt array and string functions – like array_chunk, array_combine, array_filter, array_reverse, compact etc. Here is the list
|
Function Name |
Description |
1. |
array |
creates an array |
2. |
array_fill |
fills array with values |
3. |
array_sum |
calculates the sum of array values |
4. |
asort |
sort the array and maintains index association |
5. |
arsort |
sorts the array in reverse order, maintaining index association |
6. |
sort |
sorts the array |
7. |
rsort |
sorts the array in reverse order |
8. |
uasort |
sorts the array with a user-defined comparison function |
9. |
uksort |
sorts the array by keys using the user-defined comparison function |
10. |
usort |
sorts the array by values using the user-defined comparison function |
11. |
krsort |
sorts the array by key in reverse order |
12. |
ksort |
sorts the array by key |
13. |
natsort |
sorts an array using a natural order algorithm |
14. |
natcasesort |
sorts an array using a case-insensitive natural order algorithm |
15. |
range |
creates an array containing a range of elements |
16. |
shuffle |
shuffles array’s elements |
17. |
key |
gets the key from the associative array |
18. |
in_array |
checks if a value exists in array |
19. |
sizeof |
counts the elements in the array |
20. |
count |
counts the elements in the array |
21. |
array_key_exists |
checks if the given key exists in the array |
22. |
array_keys |
returns the keys in the array |
23. |
array_pop |
pops the element off the end of the array |
24. |
array_push |
pushes one or more elements at the end of array |
25. |
next |
advances the array pointer of the array |
26. |
current |
returns the current element of the array |
27. |
pos |
returns the current element of the array |
28. |
prev |
moves the array pointer back on the element |
29. |
end |
sets the array pointer to the last element |
30. |
reset |
sets the pointer of an array to its first elements |
31. |
compact |
creates the array containing variables and their values |
32. |
extract |
imports variable into the current symbol table from an array |
33. |
list |
assigns variables as if they were an array |
34. |
array_merge |
merges two or more arrays |
35. |
array_flip |
exchanges all keys with their associated values in the array |
36. |
array_chunk |
splits an array into chunks |
37. |
array_rand |
picks one or more random elements out of the array |
38. |
array_search |
searches the array for a given value and returns the corresponding key |
39. |
array_count_values |
counts the values in an array |
40. |
array_diff |
computes the difference of arrays |
41. |
array_filter |
filters elements of an array using a callback function |
42. |
array_intersect |
computes the intersection of arrays |
43. |
array_intersect_assoc |
compute the intersection of arrays with additional values in an array |
44. |
array_combine |
creates an array by using one array for keys and another for values |
45. |
array_diff_assoc |
computes the difference of two arrays with additional index check |
46. |
array_unique |
removes duplicate elements from an array |
47. |
array_walk |
calls a user-supplied function on every member of an array |
48. |
array_values |
returns all values of an array |
49. |
array_udiff |
computes the difference between arrays by using a callback function |
50. |
array_map |
applies the callback to the elements of the given array |
51. |
array_multisort |
sorts multiple or multidimensional arrays |
52. |
array_pad |
pads array to the specified length with a value |
53. |
array_reverse |
returns an array with elements in reverse order |
54. |
array_shift |
shifts an element of the beginning of an array |
55. |
array_slice |
extracts a slice of the array |
56. |
array_splice |
removes part of the array and replaces it with something else |
57. |
implode |
implodes an array into a string |
58. |
explode |
explodes a string into an array |
Some example
<?php
$arr=array('a','b','c','d','e','e');
echo '$arr=array("a","b","c","d","e","e")<br>';
// pops an element from end of array
echo 'array_pop($arr) = ' . array_pop($arr)."<br>";
// search
echo 'array_search("c",$arr) = ' . array_search('c',$arr)."<br>";
// picks random element's key
echo 'array_rand($arr) = ' . $arr[array_rand($arr)]."<br>";
// count the elements of array
echo 'count($arr) = ' . count($arr)."<br>";
// checks if the key exists and returns Boolean
echo 'array_key_exists("3",$arr)='.array_key_exists("3",$arr);
Output
$arr=array("a","b","c","d","e","e")
array_pop($arr) = e
array_search("c",$arr) = 2
array_rand($arr) = e
count($arr) = 5
array_key_exists("3",$arr) = 1
Latest
SOLID Principles: A Comprehensive Guide with Examples
News
Pure html popover API is coming in Chromium 114
Featured
SOLID Principles: A Comprehensive Guide with Examples