PHP Arrays
An array is a special variable that can hold multiple values under a single name. Instead of creating separate variables for each item — $fruit1, $fruit2, $fruit3 — an array stores all items together in an organized structure. PHP supports three types of arrays: indexed arrays, associative arrays, and multidimensional arrays.
Indexed Arrays
An indexed array uses numeric keys starting at 0. Each element is accessed by its position number (index).
Creating an Indexed Array
<?php
// Method 1: Short array syntax
$colors = ["red", "green", "blue", "yellow"];
// Method 2: array() function
$fruits = array("apple", "banana", "cherry");
echo $colors[0]; // Outputs: red
echo $colors[2]; // Outputs: blue
echo $fruits[1]; // Outputs: banana
?>
Adding Elements
<?php
$animals = ["cat", "dog"];
$animals[] = "rabbit"; // Appends to the end (index 2)
$animals[] = "parrot"; // Now index 3
echo $animals[2]; // Outputs: rabbit
echo count($animals); // Outputs: 4
?>
Looping Through an Indexed Array
<?php
$cities = ["Paris", "Tokyo", "Sydney", "Cairo"];
foreach ($cities as $city) {
echo $city . "<br>";
}
// Also works with a for loop:
for ($i = 0; $i < count($cities); $i++) {
echo $i . ": " . $cities[$i] . "<br>";
}
?>
Associative Arrays
An associative array uses named string keys instead of numbers. This is useful when the key itself carries meaning — like a property name in a record.
<?php
$student = [
"name" => "Emma",
"age" => 22,
"grade" => "A",
"email" => "emma@example.com"
];
echo $student["name"]; // Outputs: Emma
echo $student["grade"]; // Outputs: A
?>
Adding and Updating Associative Array Elements
<?php
$product = [
"name" => "Keyboard",
"price" => 45.00
];
$product["stock"] = 150; // Adding a new key
$product["price"] = 39.99; // Updating an existing key
echo $product["price"]; // Outputs: 39.99
echo $product["stock"]; // Outputs: 150
?>
Looping Through an Associative Array
<?php
$person = [
"first_name" => "Carlos",
"last_name" => "Rivera",
"country" => "Mexico"
];
foreach ($person as $key => $value) {
echo $key . ": " . $value . "<br>";
}
// Outputs:
// first_name: Carlos
// last_name: Rivera
// country: Mexico
?>
Multidimensional Arrays
A multidimensional array contains other arrays as its elements. This creates a table-like structure — rows and columns of data.
<?php
$students = [
["Alice", 88, "A"],
["Bob", 72, "C"],
["Carol", 95, "A+"]
];
echo $students[0][0]; // Outputs: Alice
echo $students[1][1]; // Outputs: 72
echo $students[2][2]; // Outputs: A+
?>
Array of Associative Arrays
<?php
$employees = [
[
"name" => "Alice",
"department" => "Engineering",
"salary" => 75000
],
[
"name" => "Bob",
"department" => "Marketing",
"salary" => 60000
],
[
"name" => "Carol",
"department" => "Engineering",
"salary" => 80000
]
];
foreach ($employees as $employee) {
echo $employee["name"] . " - " . $employee["department"] . "<br>";
}
// Outputs:
// Alice - Engineering
// Bob - Marketing
// Carol - Engineering
?>
Accessing Nested Values
<?php
$school = [
"name" => "Westview Academy",
"address" => [
"city" => "Portland",
"state" => "Oregon",
"country" => "USA"
],
"grades" => ["9th", "10th", "11th", "12th"]
];
echo $school["name"]; // Outputs: Westview Academy
echo $school["address"]["city"]; // Outputs: Portland
echo $school["grades"][2]; // Outputs: 11th
?>
Checking if a Key or Value Exists
<?php
$config = ["debug" => true, "version" => "2.0", "timeout" => 30];
// Check if a key exists
if (array_key_exists("debug", $config)) {
echo "Debug key exists.";
}
// Check if a value exists
$fruits = ["apple", "banana", "cherry"];
if (in_array("banana", $fruits)) {
echo "Banana is in the list.";
}
?>
Removing Elements
<?php
$items = ["pen", "notebook", "eraser", "ruler"];
unset($items[2]); // Removes "eraser" (index 2)
print_r($items);
// Array ( [0] => pen [1] => notebook [3] => ruler )
// Note: Index 2 is gone; keys are not re-indexed automatically
?>
Key Points
- Indexed arrays use numeric keys starting at 0; elements are accessed via
$array[index]. - Associative arrays use named string keys; elements are accessed via
$array["key"]. - Multidimensional arrays contain arrays as elements, accessed with multiple sets of brackets.
- Use
$array[] = valueto append to an indexed array. - Use
count()to get the number of elements in an array. array_key_exists()checks for a key;in_array()checks for a value.unset()removes an element but does not re-index the remaining elements.
