PHP Array Basics Quiz
A 35-question quiz on PHP arrays covering creation, indexing, iteration, built-in helpers, references, and defensive patterns so every list, map, and collection behaves predictably.
Question 1
Why do PHP teams describe arrays as “ordered maps” when onboarding new engineers?
Question 2
What does this literal produce?
<?php
$values = ['red', 'green', 'blue'];
echo $values[1];
?>Question 3
Why do reviewers prefer short array syntax [] over array() in new codebases?
Question 4
What does this spread example echo?
<?php
$base = ['api', 'cli'];
$all = [...$base, 'queue'];
echo count($all);
?>Question 5
Why should engineering guidelines clarify whether associative arrays representing records are keyed by IDs or sequential indexes?
Question 6
What does this associative snippet echo?
<?php
$user = ['name' => 'Lina', 'role' => 'editor'];
echo $user['role'];
?>Question 7
What happens when you append to an array with $items[] = $value?
Question 8
What does this unset example output?
<?php
$list = ['a', 'b', 'c'];
unset($list[1]);
echo implode(',', $list);
?>Question 9
Why do teams lean on foreach instead of for loops when iterating over associative arrays?
Question 10
What does this foreach snippet echo?
<?php
$totals = ['draft' => 2, 'live' => 5];
foreach ($totals as $status => $count) {
echo $status . ':' . $count . ' ';
}
?>Question 11
Why should destructuring assignments list keys explicitly when pulling values from middleware payloads?
Question 12
What does this destructuring snippet echo?
<?php
['name' => $name, 'role' => $role] = ['role' => 'admin', 'name' => 'Rei'];
echo $role . '-' . $name;
?>Question 13
Why do maintainers prefer array_column for extracting IDs from database result sets?
Question 14
What does this array_column example return?
<?php
$records = [
['id' => 10, 'name' => 'Ops'],
['id' => 11, 'name' => 'Dev']
];
$ids = array_column($records, 'id');
echo implode(',', $ids);
?>Question 15
Why do reviewers highlight copy-on-write semantics when large arrays are passed by value to helper functions?
Question 16
What does this reference mutation output?
<?php
$config = ['mode' => 'api'];
$alias =& $config;
$alias['mode'] = 'cli';
echo $config['mode'];
?>Question 17
Why do bug reports often involve accidentally using + to merge arrays?
Question 18
What does this merge example output?
<?php
$a = ['id' => 1];
$b = ['id' => 2, 'name' => 'Kit'];
print_r($a + $b);
?>Question 19
Why is array_key_exists sometimes preferred over isset when checking for null values?
Question 20
What does this helper print?
<?php
$payload = ['count' => null];
echo array_key_exists('count', $payload) ? 'present' : 'missing';
?>Question 21
Why do teams prefer array_filter without reindexing when they want to preserve original keys?
Question 22
What does this filter snippet output?
<?php
$scores = [0 => 50, 1 => 80, 2 => 90];
$passed = array_filter($scores, fn ($score) => $score >= 80);
print_r($passed);
?>Question 23
Why are array_map and array_filter favored when building transformation pipelines?
Question 24
What does this array_map output?
<?php
$names = ['Lia', 'Mo'];
$upper = array_map('strtoupper', $names);
echo implode('-', $upper);
?>Question 25
Why do reviewers caution against modifying an array while iterating over it with foreach?
Question 26
What does this slicing snippet echo?
<?php
$items = ['alpha', 'beta', 'gamma', 'delta'];
$chunk = array_slice($items, 1, 2);
echo implode('|', $chunk);
?>Question 27
Why do teams document whether helper functions should return a shallow copy of an array or mutate the original?
Question 28
What does this array_reduce snippet output?
<?php
$numbers = [2, 3, 4];
$product = array_reduce($numbers, fn ($carry, $value) => $carry * $value, 1);
echo $product;
?>Question 29
Why do reviewers prefer stable sorting functions (like uasort that preserves keys) when arrays map IDs to data?
Question 30
Why might ksort be chosen over asort when preparing config arrays for deterministic output?
Question 31
Why should developers avoid count() on large iterables inside tight loops when the array size is known?
Question 32
When building pagination, why is array_slice preferred over manual loops for pulling a page of results?
Question 33
Why do teams reach for array_walk when they need to apply side-effects (such as logging) without creating a new array?
Question 34
Why should documentation specify whether array_unique is permitted on large datasets?
Question 35
Why do architecture notes emphasize returning empty arrays instead of null from repository methods that yield lists?
