Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 3 of 8
Arrays - Indexed, Associative & Multi-dimensional
PHP arrays are one of the most versatile data structures - they act as lists, maps, queues, and stacks. Every PHP application uses arrays extensively: form data, database results, API responses, and configuration all arrive as arrays. Mastering array operations is foundational.
Array Creation, Access & Core Operations
<?php
declare(strict_types=1);
// ── Indexed arrays ────────────────────────────────────────────
$tags = ["php", "backend", "web"];
$tags[] = "api"; // append
$tags[0] = "PHP"; // update by index
count($tags); // 4
// ── Associative arrays (hash maps) ────────────────────────────
$post = [
"id" => 1,
"title" => "PHP Basics",
"author" => "Alice",
"tags" => ["php", "beginner"],
"published" => true,
];
$post["views"] = 100; // add key
unset($post["published"]); // remove key
isset($post["author"]); // true
array_key_exists("views", $post); // true (safer for null values vs isset)
// ── Multi-dimensional ─────────────────────────────────────────
$blog = [
"posts" => [
["id" => 1, "title" => "PHP Basics"],
["id" => 2, "title" => "PDO Guide"],
],
"meta" => ["total" => 2, "page" => 1],
];
$blog["posts"][0]["title"]; // "PHP Basics"
$blog["meta"]["total"]; // 2
// ── Stack and Queue operations ─────────────────────────────────
$stack = [];
array_push($stack, "first", "second"); // push
$top = array_pop($stack); // "second" (LIFO)
$queue = [];
array_push($queue, "first", "second"); // enqueue
$front = array_shift($queue); // "first" (FIFO)
array_unshift($queue, "new-front"); // prepend
// ── Merging & combining ───────────────────────────────────────
$defaults = ["color" => "blue", "size" => "M", "qty" => 1];
$custom = ["color" => "red", "qty" => 3];
$options = array_merge($defaults, $custom);
// ["color" => "red", "size" => "M", "qty" => 3] - custom wins
// Spread merge (PHP 8.1 with string keys)
$merged = [...$defaults, ...$custom]; // same result
// ── Searching ─────────────────────────────────────────────────
$fruits = ["apple", "banana", "cherry"];
in_array("banana", $fruits); // true
in_array("BANANA", $fruits, true); // false (strict - type + case)
$key = array_search("cherry", $fruits); // 2
// Associative search
$user = ["role" => "admin", "active" => true];
array_key_exists("role", $user); // true
// ── Slicing and splicing ──────────────────────────────────────
$first3 = array_slice($posts, 0, 3); // get first 3 elements
$removed = array_splice($posts, 1, 1); // remove index 1, returns removed
// ── Unique and flip ──────────────────────────────────────────
$withDups = ["php", "web", "php", "backend"];
$unique = array_unique($withDups); // ["php", "web", "backend"]
$flipped = array_flip(["a" => 1, "b" => 2]); // [1 => "a", 2 => "b"]Quick Quiz
Tip
Tip
Practice Arrays Indexed Associative Multidimensional in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Arrays - Indexed, Associative & Multi-dimensional
Practice Task
Note
Practice Task - (1) Write a working example of Arrays Indexed Associative Multidimensional from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Arrays Indexed Associative Multidimensional is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?