Indexed and Associative Arrays
Create and access both types of PHP arrays.
Indexed and Associative Arrays is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Arrays in PHP
PHP arrays are ordered maps — they can act as lists, dictionaries, stacks, and queues. Two main flavors:
- Indexed arrays — numeric keys (0, 1, 2…)
- Associative arrays — string keys
Creating Indexed Arrays
Use the short array syntax [] or array():
<?php
$colors = ['red', 'green', 'blue']; // short syntax
$nums = array(10, 20, 30); // long syntax
echo $colors[0]; // red
echo $nums[2]; // 30
echo count($colors); // 3Adding and Removing Elements
Append with [], remove with unset():
<?php
$items = ['a', 'b', 'c'];
$items[] = 'd'; // append
$items[] = 'e';
unset($items[1]); // remove 'b'
print_r($items);
// [0=>'a', 2=>'c', 3=>'d', 4=>'e'] — index gap!Associative Arrays
Use string keys to create a dictionary-like structure:
<?php
$person = [
'name' => 'Alice',
'age' => 30,
'email' => 'alice@example.com',
];
echo $person['name']; // Alice
$person['city'] = 'London'; // add new key
unset($person['email']); // remove keyChecking Key Existence
Test whether a key or value exists in an array:
<?php
$config = ['debug' => false, 'version' => '2.0'];
var_dump(array_key_exists('debug', $config)); // true
var_dump(isset($config['debug'])); // false! debug=false, not NULL
var_dump(in_array('2.0', $config)); // truearray_key_exists vs isset
isset() returns false for keys with NULL values; array_key_exists() returns true even for NULL values:
<?php
$data = ['key' => null];
var_dump(isset($data['key'])); // false
var_dump(array_key_exists('key', $data)); // trueArray Unpacking
Destructure arrays into variables with the short list syntax:
<?php
$point = [10, 20, 30];
[$x, $y, $z] = $point;
echo "x=$x y=$y z=$z"; // x=10 y=20 z=30
// Skip elements
[, $second] = [1, 2, 3];
echo $second; // 2Merging Arrays
Combine arrays with array_merge() or the spread operator:
<?php
$a = ['x' => 1, 'y' => 2];
$b = ['y' => 99, 'z' => 3];
$merged = array_merge($a, $b);
print_r($merged); // x=>1, y=>99 (b overwrites), z=>3
$union = $a + $b;
print_r($union); // x=>1, y=>2 (a wins), z=>3Array Slicing
Extract a portion of an array with array_slice():
<?php
$letters = ['a', 'b', 'c', 'd', 'e'];
$slice = array_slice($letters, 1, 3);
print_r($slice); // ['b', 'c', 'd']
$last2 = array_slice($letters, -2);
print_r($last2); // ['d', 'e']Stack and Queue Operations
PHP arrays double as stacks and queues:
<?php
$stack = [];
array_push($stack, 'a', 'b');
$top = array_pop($stack); // 'b' — LIFO
$queue = ['first', 'second'];
array_push($queue, 'third');
$front = array_shift($queue); // 'first' — FIFO
array_unshift($queue, 'zero'); // prependcompact and extract
compact() builds an associative array from variable names; extract() does the reverse:
<?php
$name = 'Alice';
$age = 30;
$data = compact('name', 'age');
// ['name' => 'Alice', 'age' => 30]
extract(['city' => 'London', 'country' => 'UK']);
echo $city; // London
echo $country; // UKQuick Check
What does array_key_exists('key', $arr) return when the key exists but its value is NULL?
Recap: Indexed and Associative Arrays
PHP array fundamentals:
- Indexed arrays use numeric keys; associative use string keys
- Append with
$arr[] = value - Check keys:
array_key_exists()vsisset() - Destructure with
[$a, $b] = $arr - Merge with
array_merge()or+(different override rules) - Stack/queue ops: push, pop, shift, unshift
Frequently asked questions
Is the “Indexed and Associative Arrays” lesson free?
Yes — the full text of “Indexed and Associative Arrays” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Indexed and Associative Arrays”?
Create and access both types of PHP arrays. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Indexed and Associative Arrays” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Indexed and Associative Arrays
- Multidimensional Arrays
- Sorting and Searching Arrays
- Array Transformation Functions