Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Indexed & Associative Arrays
PHP Intermediate FREE

Indexed & Associative Arrays

Lesson 1 of 17 Intermediate Interactive

Arrays are one of the most important data structures in PHP. They can hold multiple values in a single variable.

Indexed Arrays

Use numeric indexes (starting from 0) to access elements.

Associative Arrays

Use named keys (strings) instead of numeric indexes — like a dictionary or map.

Creating Arrays

  • $arr = []; or $arr = array();
  • $arr = [1, 2, 3]; — Indexed
  • $arr = ["name" => "Ali", "age" => 25]; — Associative

Common Array Operations

  • count() — Count elements
  • array_push() — Add to end
  • array_merge() — Combine arrays
  • array_keys() — Get all keys
  • array_values() — Get all values

Syntax

PHP
<?php
// Indexed array

$fruits = ["apple", "banana", "mango"];
echo $fruits[0]; // "apple"


// Associative array

$student = [
    "name" => "Sukhwinder",
    "age" => 25,
    "course" => "PHP"
];
echo $student["name"]; // "Sukhwinder"


// Useful functions

count($fruits);          // 3

array_push($fruits, "grape");
$merged = array_merge($fruits, ["orange"]);
keys = array_keys($student);
$values = array_values($student);
?>
Indexed and Associative Arrays
PHP
<?php
// Indexed array
$colors = ["red", "green", "blue"];
echo "First: " . $colors[0] . "
";
echo "Count: " . count($colors) . "
";

$colors[] = "yellow"; // append
echo "Last: " . $colors[3] . "
";

// Associative array
$user = [
    "name" => "Alice",
    "age" => 25,
    "email" => "alice@example.com",
];
echo "Name: " . $user["name"] . "
";
echo "Age: " . $user["age"] . "
";

// Multi-dimensional
$matrix = [
    [1, 2],
    [3, 4],
];
echo "Matrix[1][1]: " . $matrix[1][1] . "
";
?>

Practice

1
Exercise

Create an associative array with 3 students (name, course, grade) and loop through them.

Answer
<?php
$students = [
    ["name" => "Ali", "course" => "PHP", "grade" => "A"],
    ["name" => "Sara", "course" => "Laravel", "grade" => "B+"],
    ["name" => "Ahmad", "course" => "MySQL", "grade" => "A-"]
];

foreach ($students as $s) {
    echo $s["name"] . " - " . $s["course"] . " (" . $s["grade"] . ")<br>";
}
?>
2
Exercise

Use array_merge() to combine two arrays and display the result.

Answer
<?php
$arr1 = ["PHP", "Laravel"];
$arr2 = ["Vue.js", "Tailwind"];
$merged = array_merge($arr1, $arr2);
echo implode(", ", $merged);
?>

Quick Quiz

1

How do you access the value with key "name" in $arr = ["name" => "Ali"]?

In associative arrays, use the string key with square brackets: $arr["name"].

2

What does array_keys() return?

array_keys() returns an array containing all the keys from the given array.

3

How do you count elements in an array?

count() returns the number of elements in an array.

Interview Questions

Indexed arrays use numeric keys (0, 1, 2...) to access elements. Associative arrays use string keys ("name", "age"...) making code more readable. Both are implemented as hash tables internally in PHP.

Both add elements to an array, but $arr[] = $value is faster because array_push() is a function call with overhead. Use $arr[] for simple appending. array_push() is useful when adding multiple elements at once.