An associative array uses named keys instead of numeric positions — the same array, just addressed by a meaningful label instead of "the third item." These show up everywhere in real PHP code, including in superglobals you'll meet soon.
<?php
$student = [
"name" => "Priya",
"age" => 21,
"course" => "Web Development",
];
echo $student["name"]; // "Priya"
?>The => symbol pairs each key with its value. Keys are usually strings, but can also be integers.
<?php
foreach ($student as $key => $value) {
echo "$key: $value\n";
}
// name: Priya
// age: 21
// course: Web Development
?>Reading a key that isn't there produces a warning, not a crash — but it's good practice to check first, especially with data from outside your own script (forms, a database, an API):
<?php
if (array_key_exists("email", $student)) {
echo $student["email"];
} else {
echo "No email on file.";
}
?>Combining arrays and associative arrays models real-world data naturally — this shape will look familiar once you reach the PHP-and-MySQL lessons, since a database row often lands in PHP exactly this way:
<?php
$students = [
["name" => "Priya", "age" => 21],
["name" => "Amit", "age" => 23],
];
echo $students[0]["name"]; // "Priya"
?>