Learn about PHP arrays and how to work with them.
|
array() and [ ] Array Creation and Syntax |
PHP provides array() and [] as notations (literals) for creating (declaring) or representing arrays. Using array() or [], you can easily create indexed arrays, associative arrays, and multidimensional arrays, or initialize arrays. |
|---|---|
|
count() Check Array Length |
The count() function function counts the number of elements (array length) in an array and returns an integer. This function also counts the number of elements in objects that implement the Countable interface and returns an integer. |
|
sizeof() Alias of count() |
The sizeof() function function counts the number of elements (array length) in an array and returns an integer. It is an alias of the count() function and works identically to it. |
|
in_array() Check if a Value Exists in an Array |
The in_array() function checks if a specific value exists in an array. It returns true if the value is found, or false otherwise. |
|
implode() Convert an Array to a String |
The implode() function joins array elements into a string using a custom delimiter. This function returns a single string by joining each element of a given array with a specified delimiter. |
|
join() Alias of implode() |
The join() function joins array elements into a single string. It is an alias of the implode() function and works identically to it. |
|
array_map() Ideal for mapping array data to new values |
The array_map() function applies the given callback function to each element of an array and returns a new array. The original array remains unchanged. |
|
array_filter() Filter Elements with a Callback |
The filter() function filters an array, keeping only the values that meet a specified condition. By providing a callback function, it returns a new array containing only the elements that satisfy the condition. |
|
array_reduce() Reducing an Array to a Single Value |
The array_reduce() function iterates through an array using a callback function to repeatedly reduce it to a single value. The original array remains unchanged. |
|
array_key_exists() Check if a Key Exists in an Array |
The array_key_exists() function checks if a specified key (or index) exists in an array. It returns true if the key is found, or false otherwise. |
|
array_keys() Get a List of Array Keys |
The array_keys() function returns a new array containing only the keys (indexes) from the given array. You can either get all keys or selectively extract only the keys that match a specific value. |
|
array_values() Get a List of Array Values |
The array_values() function returns a new array containing all the values from the given array. The returned array uses consecutive numeric indexes starting from 0, and the original keys are ignored. |
|
array_search() Searching for a Value in an Array |
The array_search() function searches an array for a value. It returns the key (or index) of the first matching element if found, or false if the value is not present. |
|
array_unique() Remove Duplicate Values from an Array |
The arrThe array_unique() function removes duplicate values from an input array and returns a new array containing only unique values. |
|
array_diff() Finding Array Differences by Value |
The array_diff() function finds the differences in array values between two or more arrays and returns a new array containing values that exist only in the first array. |
|
array_diff_assoc() Comparing Array Differences by Key–Value |
The array_diff_assoc() function compares array differences based on key–value pairs and returns an associative array containing the elements from the first array that are not present in the other arrays. |
|
array_intersect() Finding Common Values Between Arrays |
The array_intersect() function returns an array containing values from the base array that also exist in one or more other arrays. |
|
array_intersect_assoc() Find Common Elements by Key-Value |
The array_intersect_assoc() function compares arrays based on key–value pairs and returns the elements from the first array that also exist in the other arrays. |
|
array_slice() Slicing an Array |
The array_slice() function extracts a specified range of elements from an array and returns the result as a new array. |
|
array_shift() Remove the First Element from an Array |
The array_shift() function removes the first element from an array and returns it. This operation decreases the array's length by one. |
|
array_merge() Merging Arrays |
The array_merge() function merges one or more arrays in order to create a new array. The elements of each array are appended to the end of the previous array, and the function returns the merged array. |
|
array_pop() Remove the Last Element from an Array |
The array_pop() function removes the last element from an array and returns it. This operation decreases the array length by one. |
|
array_unshift() Add Elements to the Start of an Array |
The array_unshift() function prepends one or more elements to the beginning of an array—similar to inserting items at the front. This also increases the array's length. |
|
array_push() Add Elements to the End of an Array |
The array_push() function adds one or more elements to the end of an array—essentially 'pushing' them onto it, like stacking items. This also increases the array's length. |
|
array_reverse() Return Array Elements in Reverse Order |
The array_reverse() function returns a new array with the elements of a given array in reverse order. The first element becomes the last, and the last element becomes the first. |