![]() |
VOOZH | about |
Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficiently.
There are three main types of arrays in PHP:
Indexed arrays use numeric indexes starting from 0. These arrays are ideal when you need to store a list of items where the order matters.
Now, let us understand with the help of the example:
apple
You can also explicitly define numeric keys in an indexed array:
Associative arrays use named keys, which are useful when you want to store data with meaningful identifiers instead of numeric indexes.
Now, let us understand with the help of the example:
GFG
Multidimensional arrays are arrays that contain other arrays as elements. These are used to represent more complex data structures, such as matrices or tables.
Now, let us understand with the help of the example:
22
In PHP, arrays can be created using two main methods:
The traditional way of creating an array is using the array() function.
$fruits = array("apple", "banana", "cherry");In PHP 5.4 and later, you can use the shorthand [] syntax to create arrays.
$fruits = ["apple", "banana", "cherry"];You can also create associative arrays by specifying custom keys:
$person = ["name" => "GFG", "age" => 30];Note: Both methods are valid, but the shorthand syntax is preferred for its simplicity and readability.
You can access individual elements in an array using their index (for indexed arrays) or key (for associative arrays).
$fruits = ["apple", "banana", "cherry"];
echo $fruits[0]; // Outputs: apple
$person = ["name" => "GFG", "age" => 30];
echo $person["name"]; // Outputs: GFG
You can modify an existing element by assigning a new value to a specific index or key.
$fruits = ["Apple", "Banana", "Cherry"];
$fruits[1] = "Mango"; // Changes "Banana" to "Mango"
echo $fruits[1]; // Outputs: Mango
$person = ["name" => "GFG", "age" => 25];
$person["age"] = 26; // Updates the age to 26
echo $person["age"]; // Outputs: 26
You can add new elements to an array using the following methods:
$fruits = ["apple", "banana"];
array_push($fruits, "cherry"); // Adds "cherry" to the end
array_unshift($fruits, "pear"); // Adds "pear" to the beginning$person["city"] = "New York";To remove items from an array, you can use several functions:
array_pop($fruits);array_shift($fruits);unset($fruits[2]); // Removes the element with index 2PHP provides a wide range of built-in functions to work with arrays. Here are some common array functions:
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$merged = array_merge($array1, $array2);
print_r($merged); // Outputs: [1, 2, 3, 4, 5, 6]
$fruits = ["Apple", "Banana", "Cherry"];
if (in_array("Banana", $fruits)) {
echo "Banana is in the array!";
}
$numbers = [3, 1, 4, 1, 5];
sort($numbers);
print_r($numbers); // Outputs: [1, 1, 3, 4, 5]
To read about the PHP Array Functions read this article - PHP Array Function
You can loop through arrays using loops such as foreach or for.
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
$numbers = [1, 2, 3, 4];
for ($i = 0; $i < count($numbers); $i++) {
echo $numbers[$i] . "<br>";
}
To read about the PHP Array Iteration read this article - PHP Array Iteration