PHP provides a variety of array functions to manipulate and work with arrays. Here are some commonly used array functions in PHP:
array()
: Creates an array.$arr = array(1, 2, 3);
count()
: Returns the number of elements in an array.$count = count($arr);
array_push()
: Pushes one or more elements onto the end of an array.array_push($arr, 4, 5);
array_pop()
: Pops the last element off the end of an array.$lastElement = array_pop($arr);
array_shift()
: Shifts an element off the beginning of an array.$firstElement = array_shift($arr);
array_unshift()
: Prepends one or more elements to the beginning of an array.array_unshift($arr, 0, -1);
array_merge()
: Merges one or more arrays into one array.$newArray = array_merge($arr1, $arr2);
array_slice()
: Returns a portion of an array.$subset = array_slice($arr, 1, 2);
array_splice()
: Removes a portion of the array and returns it.$removed = array_splice($arr, 1, 2);
array_reverse()
: Reverses the order of the elements in an array.$reversedArray = array_reverse($arr);
array_search()
: Searches an array for a given value and returns the corresponding key if successful.$key = array_search(2, $arr);
in_array()
: Checks if a value exists in an array.$exists = in_array(3, $arr);
array_key_exists()
: Checks if the given key or index exists in the array.$exists = array_key_exists('key', $arr);
array_values()
: Returns all the values of an array.$values = array_values($arr);
array_keys()
: Returns all the keys of an array.$keys = array_keys($arr);
These are just a few examples, and PHP has many more array functions to suit various needs. Refer to the official PHP documentation for a comprehensive list and detailed information on each function.
Show Comments