An array is a special variable that can hold more than one value at a time. Instead of declaring separate variables for related items (e.g., $car1, $car2, $car3), you can store them all in a single array variable.
Arrays are one of the most powerful and frequently used data structures in PHP.
The Three Types of PHP Arrays
PHP supports three distinct types of arrays, each designed for a different way of organizing data:
- Indexed Arrays: Arrays with a numeric key (starts at
0). - Associative Arrays: Arrays with named keys (strings).
- Multidimensional Arrays: Arrays containing other arrays.
1. Indexed Arrays (Numeric Arrays)
Indexed arrays use numbers as keys to access the data. By default, the first element has the key 0, the second 1, and so on.
Creating an Indexed Array
There are two primary ways to create an indexed array:
- Automatic Indexing: Let the index numbers be assigned automatically.
- Manual Indexing: Assign the index number manually.
Example:
PHP
<?php
// Method 1: Automatic indexing
$fruits = array("Apple", "Banana", "Cherry");
// Method 2: Assigning values one by one (manual or automatic)
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[] = "Toyota"; // The index for Toyota will be automatically set to 2
?>
Accessing Array Items
You access an array item by referencing its index number inside square brackets ([]).
PHP
<?php
$fruits = array("Apple", "Banana", "Cherry");
echo "I love " . $fruits[1] . " and " . $fruits[0] . ".";
// Output: I love Banana and Apple.
?>
2. Associative Arrays
Associative arrays are the most flexible type. Instead of using numbers, you assign meaningful names (strings) as keys to your values. This makes the data easier to read and manage.
Creating an Associative Array
You use the => operator to associate a key with a value.
Example:
PHP
<?php
$student = array(
"name" => "Alice",
"age" => 25,
"course" => "PHP Development"
);
?>
Accessing Associative Array Items
You access the item by referencing its key name inside the square brackets.
PHP
<?php
$student = array("name" => "Alice", "age" => 25);
echo $student["name"] . " is " . $student["age"] . " years old.";
// Output: Alice is 25 years old.
?>
3. Looping Through Arrays
The foreach loop (introduced in the previous chapter) is specifically designed for easily iterating over all elements in an array.
Looping Indexed Arrays
PHP
<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $value) {
echo $value . "<br>";
}
?>
Looping Associative Arrays (Key and Value)
You can access both the key (name) and the value in an associative array:
PHP
<?php
$person = array("name" => "Bob", "city" => "London");
foreach ($person as $key => $value) {
echo $key . ": " . $value . "<br>";
}
/*
Output:
name: Bob
city: London
*/
?>
4. Multidimensional Arrays
A multidimensional array is an array that contains one or more arrays inside it. This is useful for storing complex, structured data (like a table or a list of records).
Example (A table of cars):
PHP
<?php
$cars = array(
array("Volvo", 22, 18), // Row 0
array("BMW", 15, 13), // Row 1
array("Audi", 5, 2) // Row 2
);
// Access the number of units sold (index 1) for the BMW (row 1)
echo $cars[1][1];
// Output: 15
?>
Next Steps
Arrays are the foundation of data handling. The next natural step is to learn about Functions, which allow you to group code into reusable blocks, significantly improving the organization and efficiency of your programming projects.
