Associative Arrays

Introduction

Associative arrays in PHP are arrays that use named keys instead of numeric indexes. These keys help store and access data in a meaningful and organized way. Associative arrays are commonly used to manage user information, product details, settings, and database records.

Objectives

By the end of this training, you will be able to:

  • Understand associative arrays in PHP
  • Create and use associative arrays
  • Access array values using keys
  • Update associative array data
  • Loop through associative arrays
  • Use associative arrays in real-world applications

What is an Associative Array

An associative array stores data in key-value pairs.

Syntax:

$arrayName = array(
"key" => "value"
);

Example:

<?php
$student = array(
"name" => "Ali",
"age" => 20,
"city" => "Lahore"
);

echo $student["name"];
?>

Output:

Ali

Creating Associative Arrays

You can create associative arrays using the array() function or square brackets.

Using array():

<?php
$employee = array(
"id" => 101,
"name" => "Ahmed",
"department" => "IT"
);
?>

Using square brackets:

<?php
$product = [
"name" => "Laptop",
"price" => 50000,
"brand" => "Dell"
];
?>

Accessing Associative Array Values

Use the key name to access a value.

<?php
$user = [
"username" => "admin",
"email" => "admin@example.com"
];

echo $user["email"];
?>

Updating Associative Arrays

You can change existing values or add new values.

<?php
$car = [
"brand" => "Toyota",
"model" => "Corolla"
];

$car["model"] = "Yaris";
$car["color"] = "White";

print_r($car);
?>

Looping Through Associative Arrays

Using foreach Loop

<?php
$student = [
"name" => "Sara",
"age" => 22,
"course" => "Web Development"
];

foreach($student as $key => $value) {
echo $key . ": " . $value . "<br>";
}
?>

Output:

name: Sara
age: 22
course: Web Development

Multidimensional Associative Arrays

Associative arrays can contain other arrays.

<?php
$employees = [
"emp1" => [
"name" => "Ali",
"department" => "HR"
],
"emp2" => [
"name" => "Ahmed",
"department" => "Finance"
]
];

echo $employees["emp1"]["name"];
?>

Common Associative Array Functions

count()

Returns the number of elements.

<?php
$colors = [
"first" => "Red",
"second" => "Blue"
];

echo count($colors);
?>

array_keys()

Returns all keys from the array.

<?php
$data = [
"name" => "Ali",
"city" => "Karachi"
];

print_r(array_keys($data));
?>

array_values()

Returns all values from the array.

<?php
$data = [
"name" => "Ali",
"city" => "Karachi"
];

print_r(array_values($data));
?>

Real World Uses of Associative Arrays

Associative arrays are used in:

  • User profile management
  • Product information systems
  • Student record systems
  • Website settings
  • Shopping carts
  • Database result handling

Advantages of Associative Arrays

  • Easy to organize data
  • Meaningful key names improve readability
  • Faster data access using keys
  • Useful for structured information
  • Ideal for dynamic web applications

Best Practices

  • Use descriptive key names
  • Keep array structure consistent
  • Avoid duplicate keys
  • Use loops for efficient data handling
  • Validate array keys before accessing values

Final Presentation

In your final presentation, explain:

  • What associative arrays are
  • Difference between indexed and associative arrays
  • How to create associative arrays
  • Accessing and updating values
  • Using loops with associative arrays
  • Real-world applications of associative arrays
Home Ā» Intermediate PHP > Arrays > Associative Arrays