Array Basics in PHP

From the PHP cheat sheet · Arrays · verified Jul 2026

Array Basics

Creating and accessing arrays

php
// Indexed array
$numbers = [1, 2, 3, 4, 5];
$colors = array('red', 'green', 'blue');

// Associative array
$person = [
    'name' => 'John',
    'age' => 30,
    'email' => 'john@example.com'
];

// Multidimensional array
$matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

// Accessing elements
echo $numbers[0];  // 1
echo $person['name'];  // John
echo $matrix[1][2];  // 6
💡 Use [] syntax for arrays (modern PHP)
⚡ Spread operator (...) unpacks arrays
📌 array_key_exists vs isset: isset returns false for null
🔥 Destructuring makes code cleaner

More PHP tasks

Back to the full PHP cheat sheet