What you'll learn
PHP arrays are ordered maps: one type can represent a list, a key-value record, or nested data. You'll learn to shape arrays deliberately and transform them without losing clarity.
- Create indexed and associative arrays
- Safely read, add, update, and remove values
- Model nested data
- Map, filter, and reduce collections
- Sort, destructure, and spread values
Lists and records
Integer keys naturally model ordered lists. String keys model named fields. Both use the short [...] syntax.
<?php
$topics = ['syntax', 'arrays', 'functions'];
$student = [
'name' => 'Nour',
'level' => 'beginner',
'completed' => 3,
];
echo $topics[1] . PHP_EOL;
echo $student['name'];Key idea
Read and change values safely
<?php
$cart = ['book', 'pen'];
$cart[] = 'notebook'; // append
$cart[1] = 'blue pen'; // update
unset($cart[0]); // remove; remaining keys are not reset
$profile = ['name' => 'Nour'];
$profile['city'] = 'Beirut';
echo count($cart) . PHP_EOL;
echo $profile['city'];| Question | Tool |
|---|---|
| Does this key exist, even if its value is null? | array_key_exists($key, $array) |
| Does this key exist and contain a non-null value? | isset($array[$key]) |
| Does this list contain a value? | in_array($value, $array, true) |
| How many elements are present? | count($array) |
Tip
true as the third argument to in_array() for strict comparison. It avoids treating values such as 0 and "0" as interchangeable.Nested and multidimensional data
<?php
$students = [
['name' => 'Nour', 'scores' => [82, 91]],
['name' => 'Ziad', 'scores' => [76, 88]],
];
echo $students[0]['name'] . ': ' . $students[0]['scores'][1];Nested arrays mirror JSON-like data well. When a shape carries important business rules or behavior, a class will eventually communicate the model better; that arrives in Phase 3.
Map, filter, and reduce
array_map() transforms every item, array_filter() keeps matching items, and array_reduce() combines a collection into one result.
<?php
$prices = [12.50, 8.00, 22.40, 5.00];
$salePrices = array_map(
fn (float $price): float => $price * 0.9,
$prices
);
$overTen = array_filter(
$salePrices,
fn (float $price): bool => $price > 10
);
$total = array_reduce($overTen, fn ($sum, $price) => $sum + $price, 0.0);
echo number_format($total, 2);Sort, destructure, and spread
<?php
$scores = [72, 95, 81];
rsort($scores);
[$highest, $second] = $scores;
$allScores = [...$scores, 68, 90];
echo "{$highest}, {$second}" . PHP_EOL;
echo implode(', ', $allScores);Sorting can change keys
sort() and rsort() reindex numeric keys. Associative sorting functions such as asort() preserve key associations. Choose by the data contract you need.Recap & quick check
Key takeaways
- PHP arrays are ordered maps that can represent lists or named records.
- Use isset, array_key_exists, and strict in_array for different existence questions.
- Nested arrays model structured data, but important domain behavior may deserve a class.
- Map transforms, filter selects, and reduce combines.
- Know whether a sorting function preserves keys before using it.
Quick check
1. Which syntax appends an item to a list?
2. Which check distinguishes an existing null key from a missing key?
3. Which operation keeps only matching elements?