PHP Array Basics Quiz

PHP
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

Why do PHP teams describe arrays as “ordered maps” when onboarding new engineers?

A
Because arrays can store values under numeric or string keys while preserving insertion order.
B
Because arrays only store integers.
C
Because arrays automatically sort themselves alphabetically.
D
Because arrays cannot hold nested structures.
2

Question 2

What does this literal produce?

php
<?php
$values = ['red', 'green', 'blue'];
echo $values[1];
?>
A
green
B
red
C
blue
D
Undefined offset notice
3

Question 3

Why do reviewers prefer short array syntax [] over array() in new codebases?

A
It keeps literals concise and matches the style adopted since PHP 5.4.
B
array() has been removed from PHP.
C
Short syntax automatically sorts keys.
D
[] forces immutability.
4

Question 4

What does this spread example echo?

php
<?php
$base = ['api', 'cli'];
$all = [...$base, 'queue'];
echo count($all);
?>
A
3
B
2
C
queue
D
api
5

Question 5

Why should engineering guidelines clarify whether associative arrays representing records are keyed by IDs or sequential indexes?

A
Knowing the key scheme prevents accidental overrides and clarifies iteration patterns.
B
PHP refuses to mix numeric and string keys.
C
Associative arrays cannot be iterated.
D
Keys determine memory allocation in INI files.
6

Question 6

What does this associative snippet echo?

php
<?php
$user = ['name' => 'Lina', 'role' => 'editor'];
echo $user['role'];
?>
A
editor
B
Lina
C
role
D
Array
7

Question 7

What happens when you append to an array with $items[] = $value?

A
PHP chooses the next highest integer key, even if earlier numeric keys were removed.
B
PHP resets keys to zero each time.
C
The value replaces the last element.
D
The array becomes immutable.
8

Question 8

What does this unset example output?

php
<?php
$list = ['a', 'b', 'c'];
unset($list[1]);
echo implode(',', $list);
?>
A
a,,c
B
abc
C
a,c
D
b
9

Question 9

Why do teams lean on foreach instead of for loops when iterating over associative arrays?

A
foreach handles keys automatically and avoids manual index tracking, reducing off-by-one bugs.
B
for loops are deprecated.
C
foreach runs in parallel automatically.
D
for loops cannot access values.
10

Question 10

What does this foreach snippet echo?

php
<?php
$totals = ['draft' => 2, 'live' => 5];
foreach ($totals as $status => $count) {
    echo $status . ':' . $count . ' ';
}
?>
A
draft:2 live:5
B
2:0 5:1
C
draft live
D
2 5
11

Question 11

Why should destructuring assignments list keys explicitly when pulling values from middleware payloads?

A
Explicit keys fail loudly if the structure changes, preventing silent bugs.
B
Destructuring only works with numeric arrays.
C
Destructuring renames keys automatically.
D
Destructuring mutates the source array.
12

Question 12

What does this destructuring snippet echo?

php
<?php
['name' => $name, 'role' => $role] = ['role' => 'admin', 'name' => 'Rei'];
echo $role . '-' . $name;
?>
A
admin-Rei
B
Rei-admin
C
role-name
D
Notice: Undefined index
13

Question 13

Why do maintainers prefer array_column for extracting IDs from database result sets?

A
array_column communicates intent clearly and is optimized in C for common extraction tasks.
B
array_column sorts the data automatically.
C
array_column converts values to strings.
D
array_column is required before JSON encoding.
14

Question 14

What does this array_column example return?

php
<?php
$records = [
    ['id' => 10, 'name' => 'Ops'],
    ['id' => 11, 'name' => 'Dev']
];
$ids = array_column($records, 'id');
echo implode(',', $ids);
?>
A
10,11
B
Ops,Dev
C
id,id
D
Array
15

Question 15

Why do reviewers highlight copy-on-write semantics when large arrays are passed by value to helper functions?

A
Mutating a copied array forces PHP to duplicate memory; passing by reference or slicing carefully avoids unexpected overhead.
B
Arrays never copy themselves.
C
References are forbidden in PHP.
D
Copy-on-write only applies to strings.
16

Question 16

What does this reference mutation output?

php
<?php
$config = ['mode' => 'api'];
$alias =& $config;
$alias['mode'] = 'cli';
echo $config['mode'];
?>
A
cli
B
api
C
mode
D
Array
17

Question 17

Why do bug reports often involve accidentally using + to merge arrays?

A
The + operator preserves keys from the left-hand array and ignores duplicates, which surprises developers expecting concatenation.
B
The + operator sorts arrays descending.
C
The + operator is deprecated.
D
The + operator only works for numeric arrays.
18

Question 18

What does this merge example output?

php
<?php
$a = ['id' => 1];
$b = ['id' => 2, 'name' => 'Kit'];
print_r($a + $b);
?>
A
Array ( [id] => 1 [name] => Kit )
B
Array ( [id] => 2 [name] => Kit )
C
Array ( [id] => Array )
D
Array ( [0] => 1 [1] => 2 )
19

Question 19

Why is array_key_exists sometimes preferred over isset when checking for null values?

A
isset returns false for null values, whereas array_key_exists confirms the key exists even if it stores null.
B
array_key_exists is faster than isset in all cases.
C
isset throws warnings when keys exist.
D
array_key_exists works on strings only.
20

Question 20

What does this helper print?

php
<?php
$payload = ['count' => null];
echo array_key_exists('count', $payload) ? 'present' : 'missing';
?>
A
present
B
missing
C
null
D
Notice: Undefined index
21

Question 21

Why do teams prefer array_filter without reindexing when they want to preserve original keys?

A
array_filter keeps the original keys by default, making it easy to map results back to their sources.
B
array_filter sorts keys automatically.
C
array_filter converts keys to strings.
D
array_filter always reindexes unless you pass a flag.
22

Question 22

What does this filter snippet output?

php
<?php
$scores = [0 => 50, 1 => 80, 2 => 90];
$passed = array_filter($scores, fn ($score) => $score >= 80);
print_r($passed);
?>
A
Array ( [1] => 80 [2] => 90 )
B
Array ( [0] => 50 )
C
Array ( [0] => 80 [1] => 90 )
D
Array ( )
23

Question 23

Why are array_map and array_filter favored when building transformation pipelines?

A
They keep transformation logic declarative, avoid manual accumulation, and integrate well with anonymous functions.
B
They run inside the database automatically.
C
They are the only functions allowed under PSR-12.
D
They mutate the original array in place.
24

Question 24

What does this array_map output?

php
<?php
$names = ['Lia', 'Mo'];
$upper = array_map('strtoupper', $names);
echo implode('-', $upper);
?>
A
LIA-MO
B
Lia-Mo
C
Array
D
LIALia
25

Question 25

Why do reviewers caution against modifying an array while iterating over it with foreach?

A
Mutations can skip elements or duplicate work because foreach uses an internal pointer that may not reflect added/removed elements immediately.
B
foreach throws exceptions on mutation.
C
PHP copies the array on every iteration regardless.
D
foreach only supports immutable arrays.
26

Question 26

What does this slicing snippet echo?

php
<?php
$items = ['alpha', 'beta', 'gamma', 'delta'];
$chunk = array_slice($items, 1, 2);
echo implode('|', $chunk);
?>
A
beta|gamma
B
alpha|beta
C
gamma|delta
D
beta|gamma|delta
27

Question 27

Why do teams document whether helper functions should return a shallow copy of an array or mutate the original?

A
Callers need to know whether they can reuse the original data without unexpected changes.
B
PHP forbids returning arrays by value.
C
Mutating arrays automatically clears caches.
D
Copying arrays removes all keys.
28

Question 28

What does this array_reduce snippet output?

php
<?php
$numbers = [2, 3, 4];
$product = array_reduce($numbers, fn ($carry, $value) => $carry * $value, 1);
echo $product;
?>
A
24
B
9
C
1
D
0
29

Question 29

Why do reviewers prefer stable sorting functions (like uasort that preserves keys) when arrays map IDs to data?

A
Preserving keys avoids losing the relationship between IDs and payloads after sorting.
B
Stable sorts run in constant time.
C
uasort automatically deduplicates entries.
D
Stable sorts enforce strict typing.
30

Question 30

Why might ksort be chosen over asort when preparing config arrays for deterministic output?

A
ksort sorts by keys, ensuring consistent ordering even when values repeat.
B
ksort automatically converts keys to integers.
C
ksort removes duplicate entries.
D
ksort sorts by values descending.
31

Question 31

Why should developers avoid count() on large iterables inside tight loops when the array size is known?

A
count() runs in O(1) but repeated calls add overhead; caching the size is clearer and avoids function calls.
B
count() mutates the array.
C
count() is deprecated.
D
count() throws exceptions for arrays larger than 1000 elements.
32

Question 32

When building pagination, why is array_slice preferred over manual loops for pulling a page of results?

A
array_slice expresses the intent directly, handles offsets correctly, and avoids boilerplate counters.
B
array_slice automatically queries the database.
C
Manual loops cannot skip elements.
D
array_slice mutates the input array.
33

Question 33

Why do teams reach for array_walk when they need to apply side-effects (such as logging) without creating a new array?

A
array_walk iterates in place, letting the callback perform actions while keeping the original structure intact.
B
array_walk automatically reindexes arrays.
C
array_walk is faster than foreach.
D
array_walk serializes arrays by default.
34

Question 34

Why should documentation specify whether array_unique is permitted on large datasets?

A
array_unique reindexes and compares values, which can become expensive; teams may prefer deduplication upstream.
B
array_unique destroys associative keys permanently.
C
array_unique only works on strings.
D
array_unique converts all values to integers.
35

Question 35

Why do architecture notes emphasize returning empty arrays instead of null from repository methods that yield lists?

A
An empty array keeps call sites simple—foreach and count still work—while null forces conditional checks everywhere.
B
Null cannot be serialized.
C
Empty arrays consume no memory.
D
PHP forbids returning null from functions.

QUIZZES IN PHP