PHP Associative Arrays
markdown
Associative arrays are one of the most useful data structures in PHP. Unlike indexed arrays that use numeric keys, associative arrays use named keys, making your data easier to understand, access, and manage.
In this tutorial, you will learn:
What associative arrays are
How to create them
How to access and update values
How to loop through associative arrays
Useful built-in functions
Real-world developer examples
A PHP associative array stores data in a key–value format.
$user = [
"name" => "Shubham",
"email" => "shubham@example.com",
"role" => "Developer"
];
Here:
Keys → name, email, role
Values → Shubham, shubham@example.com, Developer
Associative arrays are perfect when you want to reference data using meaningful names instead of numbers.
$student = [
"id" => 101,
"name" => "Amit",
"course" => "PHP",
"marks" => 85
];
array() Function
$student = array(
"id" => 101,
"name" => "Amit",
"course" => "PHP",
"marks" => 85
);
Both methods work exactly the same.
Access array elements using their key.
echo $student["name"]; // Amit
echo $student["course"]; // PHP
$user = [
"name" => "Rahul",
"email" => "rahul@example.com"
];
$user["role"] = "Admin"; // Add new value
$user["email"] = "rahul.dev@example.com";
foreach Loop (Most Common)
$user = [
"name" => "Sneha",
"email" => "sneha@example.com",
"city" => "Pune"
];
foreach ($user as $key => $value) {
echo $key . " : " . $value . "<br>";
}
Output:
name : Sneha
email : sneha@example.com
city : Pune
$product = [
"id" => 5001,
"name" => "Wireless Mouse",
"price" => 799,
"in_stock" => true
];
echo "Product: " . $product["name"] . "<br>";
echo "Price: ₹" . $product["price"] . "<br>";
echo $product["in_stock"] ? "Status: Available" : "Status: Out of Stock";
Associative arrays can contain other arrays.
$users = [
"user1" => [
"name" => "Shubham",
"email" => "shubham@example.com"
],
"user2" => [
"name" => "Priya",
"email" => "priya@example.com"
]
];
echo $users["user1"]["name"]; // Shubham
array_keys() – Get All Keys
array_keys($user);
array_values() – Get All Values
array_values($user);
array_key_exists() – Check if Key Exists
array_key_exists("email", $user);
isset() – Check if Key Exists AND Not Null
isset($user["city"]);
| Feature | Indexed Array | Associative Array |
|---|---|---|
| Keys | Numeric | Named (Strings) |
| Readability | Lower | Higher |
| Best For | Lists | Structured data |
PHP associative arrays are powerful and easy to use. They provide readable keys, flexible data storage, and are essential when working with user profiles, database records, product details, and structured data.
Using associative arrays will make your PHP applications more organized, efficient, and professional.
Learn PHP associative arrays with clear examples. Understand how to create, access, loop, and use associative arrays in real-world PHP applications.
php associative arrays, php arrays tutorial, php key value array, php beginner guide, php array example
"PHP associative array example with key-value pairs"