haphpiness

These are things in PHP which make me genuinely_happy();

Array Destructuring with Keys — Pattern Matching for Arrays

PHP's short array syntax supports key-based destructuring: pull specific values out of an associative array by name, discarding the rest. It reads like pattern matching and eliminates a class of "which index is that?" bugs.

// Extract specific keys from an associative array
$person = ['name' => 'Alice', 'age' => 30, 'city' => 'London'];

['name' => $name, 'age' => $age] = $person;

echo $name;  // "Alice"
echo $age;   // 30
// 'city' is ignored — only extract what you need

This shines in foreach loops over result sets, where each row is an associative array:

$rows = [
    ['id' => 1, 'email' => 'alice@example.com', 'role' => 'admin'],
    ['id' => 2, 'email' => 'bob@example.com',   'role' => 'user'],
    ['id' => 3, 'email' => 'carol@example.com', 'role' => 'user'],
];

foreach ($rows as ['id' => $id, 'email' => $email]) {
    echo "$id: $email\n";
    // 'role' is irrelevant here — just ignore it
}

You can also use the list() form with keys, and nesting works for deeper structures:

// Nested destructuring
$config = [
    'database' => ['host' => 'localhost', 'port' => 5432],
    'cache'    => ['driver' => 'redis'],
];

['database' => ['host' => $host, 'port' => $port]] = $config;
echo "$host:$port";  // "localhost:5432"

Significance: Expressiveness

Destructuring with keys bridges the gap between PHP's associative arrays and pattern matching. Instead of $row['email'] repeated five times in a loop body, you bind once and read a clean name throughout — and the intent is visible at the top of the block.