1. Understanding Arrays in PHP
An array is an ordered data structure that stores multiple values within a single variable. PHP supports three primary array formats:
2. Indexed Arrays
Indexed arrays use numeric indices starting from 0:
";
}
3. Associative Arrays
Associative arrays use named keys (strings) to access stored values:
"Priya Sharma",
"email" => "priya@example.com",
"course" => "NEET Preparation",
"isActive" => true
];
echo $student["name"]; // Priya Sharma
// Adding a new key-value pair
$student["enrollment_year"] = 2026;
// Iterating key and value pairs
foreach ($student as $key => $val) {
echo ucfirst($key) . ": " . (is_bool($val) ? ($val ? 'Yes' : 'No') : $val) . "
";
}
4. Multidimensional Arrays
A multidimensional array is an array containing one or more arrays. They are commonly used to represent database records or matrix grids:
1, "name" => "Aarav", "score" => 98],
["rank" => 2, "name" => "Sneha", "score" => 95],
["rank" => 3, "name" => "Rohan", "score" => 91],
];
// Accessing Sneha's score:
echo $examLeaderboard[1]["score"]; // 95