This tutorial lists out topics that cover Array Operations in PHP like creating arrays, modifying or transforming arrays, etc.
PHP Array Programs
Array Basics
- PHP – Introduction to Arrays An introduction to arrays, and types of arrays in PHP language.
- PHP – Indexed arrays PHP Tutorial about arrays in which the elements are ordered based on index.
- PHP – Associative arrays PHP Tutorial about arrays in which the values are associated with the keys.
- PHP – 2D Array PHP Tutorial about two dimensional arrays.
- PHP – Multi-dimensional arrays PHP Tutorial about multi-dimensional arrays.
- PHP – Array of strings PHP Tutorial about array of strings, where all the elements of an array are strings.
Array Create Operations
- PHP – Create an indexed array PHP Tutorial to create an indexed array using array() function.
- PHP – Create an associative array PHP Tutorial to create an associative array using array() function.
- PHP – Create an empty array PHP Tutorial to create an empty array using array() function or square bracket notation.
Array Properties
- PHP – Array length PHP Tutorial to get the number of elements in the given array using count() function.
- PHP – Get keys of an array PHP tutorial to get the keys of an array, using array_keys() function.
Array Checks
- PHP – Check if array is empty PHP Tutorial to check if array is empty by checking the number of elements in the array.
- PHP – Check if specific element is present in array PHP Tutorial to check if array contains a specific element using in_array() function.
- PHP – Check if two arrays are equal PHP Tutorial to check if two arrays are equal using array identity operator.
- PHP – Check if any two adjacent values are same PHP Tutorial to check if any two adjacent values are same using For loop and Equal-to operator.
Array Read/Access Operations
- PHP – Access array elements using index PHP Tutorial to access individual elements in an array using the index.
- PHP – Iterate through array using For loop PHP Tutorial to iterate through the elements of an array using For loop and indices.
- PHP – Array foreach() PHP Tutorial to execute a block of code for each element in the array using for foreach loop statement.
- PHP – Iterate over key-value pairs of associative array PHP Tutorial to iterate over the key-value pairs of the given associative array using foreach statement.
- PHP – Get first element in array PHP Tutorial to get first element in the array using array[index] notation.
- PHP – Get last element in array PHP Tutorial to get last element in the array using array[index] notation.
- PHP – Slice array by index PHP Tutorial to slice an array by index using array_slice() function.
- PHP – Slice array by key PHP Tutorial to slice an array by index using array_slice() function and a little help from array_search() function.
Array Find Operations
- PHP – Count occurrences of specific value in array PHP Tutorial to count the number of occurrences of specific value in the given array using count() and array_filter() functions.
- PHP – Find index of value in array PHP Tutorial to find the index of first occurrence of specific element in array using array_search() function.
- PHP – Find index of key in array PHP Tutorial to find the index of specific key in array using array_search() function.
- PHP – Find index of last occurrence of value in array PHP Tutorial to find the index of last occurrence of specific element in array using array_reverse() and array_search() functions.
Array Transformations
- PHP – Combine two arrays to create an Associative Array PHP Tutorial to use array_combine() function to combine two arrays (first array is for keys, second array is for values) to form an associative array.
- PHP – Reverse an array PHP Tutorial to reverse the order of elements in given array using array_reverse() function.
- PHP – Sort array PHP Tutorial to sort an array in ascending or descending order using sort() or rsort() functions respectively.
- PHP – Split array into chunks PHP Tutorial to split given array into smaller arrays.
- PHP – Truncate array PHP Tutorial to truncate an array to specific length using array_splice() function.
Array Filtering
The following tutorials use array_filter() function to filter element in the array.
- PHP – Remove duplicate values in array PHP Tutorial to remove duplicate values in an array using array_unique() function.
- PHP – Filter elements in array based on a condition PHP Tutorial to filter elements in an array based on a condition (given in a callback function).
- PHP – Filter positive numbers in array PHP Tutorial to keep only positive numbers in an array using array_filter() function.
- PHP – Filter negative numbers in array PHP Tutorial to keep only negative numbers in an array.
- PHP – Remove empty strings in array PHP Tutorial to remove empty strings in a string array.
Array Delete Operations
- PHP – Delete specific value from array PHP Tutorial to remove all the occurrences of a specific element in an array using array_filter() function.
- PHP – Delete value at specific index from array PHP Tutorial to delete an element at specific index from array using unset() function.
- PHP – Remove first value in array PHP Tutorial to remove first element in the array using unset() function.
- PHP – Remove last value in array PHP Tutorial to remove last element in the array using unset() function.
Exceptions
- PHP Array – Notice: Undefined offset: 0 PHP Tutorial to address and solve the issue raised when we try to access element from array with an offset (index) that is not present in the array.
Other Tutorials
- PHP – count() function
- PHP – array_change_key_case() function
- PHP – array_chunk() function
- PHP – array_column() function
- PHP – array_combine() function
- PHP – array_count_values() function
- PHP – array_diff_assoc() function
- PHP – array_diff_key() function
- PHP – array_diff_uassoc() function
- PHP – array_diff_ukey() function