codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

21 / 56 topics
19Arrays in PHP20Indexed Arrays21Associative Arrays22Multidimensional Arrays23Array Functions
Tutorials/PHP/Associative Arrays
🐘PHP

Associative Arrays

Updated 2026-05-15
10 min read

Associative Arrays

Introduction

In PHP, arrays are a fundamental data structure that can hold multiple values. These values can be accessed using numeric indices or named keys. Numeric indexed arrays are the most common type of array in PHP, but there is another powerful type called associative arrays. Associative arrays allow you to store and access data by key names instead of numerical indices. This makes your code more readable and easier to manage.

Concept

An associative array in PHP is an array where each element is associated with a key. The keys can be strings or integers, but they are typically used as strings for clarity and readability. Associative arrays are created using the array() function or the short array syntax [].

Creating Associative Arrays

You can create an associative array by specifying key-value pairs within the array syntax. Here’s how you can do it:

Using the array() function

$person = array(
    "name" => "John",
    "age" => 30,
    "city" => "New York"
);

Using the short array syntax

$person = [
    "name" => "John",
    "age" => 30,
    "city" => "New York"
];

Accessing Elements in Associative Arrays

You can access elements of an associative array using their keys. Here’s how you can do it:

echo $person["name"]; // Outputs: John
echo $person["age"];  // Outputs: 30
echo $person["city"]; // Outputs: New York

Adding Elements to Associative Arrays

You can add new elements to an associative array by simply assigning a value to a new key:

$person["email"] = "john@example.com";

Modifying Elements in Associative Arrays

To modify an existing element, you can assign a new value to its key:

$person["age"] = 31; // Changes the age from 30 to 31

Removing Elements from Associative Arrays

You can remove elements using the unset() function:

unset($person["city"]); // Removes the "city" element

Examples

Let’s go through some practical examples to solidify our understanding of associative arrays.

Example 1: Creating and Accessing an Associative Array

$student = [
    "name" => "Alice",
    "grade" => "A",
    "age" => 20
];

echo "Student Name: " . $student["name"] . "\n";
echo "Grade: " . $student["grade"] . "\n";
echo "Age: " . $student["age"] . "\n";
Output
Student Name: Alice  
Grade: A  
Age: 20

Example 2: Adding and Modifying Elements

$book = [
    "title" => "1984",
    "author" => "George Orwell"
];

// Add a new element
$book["year"] = 1949;

// Modify an existing element
$book["title"] = "Animal Farm";

print_r($book);
Output
Array  
(  
  [title] => Animal Farm  
  [author] => George Orwell  
  [year] => 1949  
)

Example 3: Removing Elements

$car = [
    "make" => "Toyota",
    "model" => "Corolla",
    "color" => "red"
];

// Remove the color element
unset($car["color"]);

print_r($car);
Output
Array  
(  
  [make] => Toyota  
  [model] => Corolla  
)

What's Next?

In the next section, we will explore multidimensional arrays in PHP. Multidimensional arrays are arrays that contain other arrays and allow you to create more complex data structures.

Stay tuned for more tutorials on PHP!


PreviousIndexed ArraysNext Multidimensional Arrays

Recommended Gear

Indexed ArraysMultidimensional Arrays