In PHP, an array is a data structure that allows you to store multiple values in a single variable. There are different types of arrays in PHP, including indexed arrays, associative arrays, and multidimensional arrays. Arrays are useful when you need to work with collections of data, such as a list of items, key-value pairs, or tables.
Types of Arrays in PHP
- Indexed Arrays
- Associative Arrays
- Multidimensional Arrays
1. Indexed Arrays
An indexed array uses numeric indexes (starting from 0 by default) to store values. The values are stored in a specific order, and each value is assigned an index that corresponds to its position in the array.
Syntax:
Or using the short array syntax:
Example:
- In this example, the array
$fruits
contains 3 items. The first item ("Apple") is at index0
, the second item ("Banana") is at index1
, and the third item ("Cherry") is at index2
.
2. Associative Arrays
An associative array uses named keys (strings) rather than numeric indexes to store values. This allows you to map a unique key to each value.
Syntax:
Or using the short array syntax:
Example:
- In this example, the array
$person
uses keys like "name", "age", and "city" to store their respective values.
3. Multidimensional Arrays
A multidimensional array is an array that contains other arrays. It's used to store more complex data structures, such as tables or grids.
Syntax:
Example:
- In this example,
$matrix
is a 2D array (array of arrays). You can access each element by specifying two indexes: the first for the outer array and the second for the inner array.
Common Array Functions in PHP
PHP provides many built-in functions to work with arrays. Here are some of the most commonly used ones:
1. count()
Returns the number of elements in an array.
2. array_push()
Adds one or more elements to the end of an array.
3. array_pop()
Removes the last element from an array.
4. array_shift()
Removes the first element from an array.
5. array_unshift()
Adds one or more elements to the beginning of an array.
6. in_array()
Checks if a value exists in an array.
7. array_keys()
Returns all the keys of an array.
8. array_values()
Returns all the values of an array.
9. array_merge()
Merges two or more arrays.
10. array_slice()
Extracts a portion of an array.
Accessing Array Elements
You can access array elements using their index (for indexed arrays) or key (for associative arrays).
Example 1: Indexed Array
Example 2: Associative Array
Summary
- Indexed Arrays: Use numeric indexes to access values. Good for lists or collections with no key-value pairs.
- Associative Arrays: Use named keys to access values. Useful for mapping data to descriptive keys.
- Multidimensional Arrays: Arrays inside arrays. Used for more complex data structures (e.g., tables or grids).
- Common Array Functions: PHP provides numerous functions like
count()
,array_push()
,array_pop()
,array_shift()
, etc., to manipulate and interact with arrays.
Arrays are an essential data structure in PHP and are used extensively to handle large collections of data efficiently.