Bird
Raised Fist0
PHPprogramming~20 mins

Why array functions matter in PHP - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of array_map with anonymous function
What is the output of this PHP code?
PHP
<?php
$numbers = [1, 2, 3, 4];
$result = array_map(fn($n) => $n * 2, $numbers);
print_r($result);
?>
AArray ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
BArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
CArray ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )
DSyntax error
Attempts:
2 left
💡 Hint
array_map applies the function to each element in the array.
❓ Predict Output
intermediate
2:00remaining
Result of array_filter with callback
What does this PHP code output?
PHP
<?php
$values = [10, 15, 20, 25, 30];
$filtered = array_filter($values, fn($v) => $v > 20);
print_r($filtered);
?>
AArray ( [0] => 10 [1] => 15 [2] => 20 )
BFatal error: Uncaught TypeError
CArray ( [0] => 10 [1] => 15 [2] => 20 [3] => 25 [4] => 30 )
DArray ( [3] => 25 [4] => 30 )
Attempts:
2 left
💡 Hint
array_filter keeps elements where the callback returns true.
🔧 Debug
advanced
2:00remaining
Output of array_reduce without initial value
This PHP code is intended to sum all numbers in the array. What is the output?
PHP
<?php
$nums = [1, 2, 3, 4];
$sum = array_reduce($nums, fn($carry, $item) => $carry + $item);
echo $sum;
?>
AFatal error: Uncaught TypeError
BNotice: Undefined variable: carry
COutput: 10
DWarning: array_reduce() expects parameter 3 to be mixed, none given
Attempts:
2 left
💡 Hint
array_reduce uses the first element as the initial carry when not specified and the array is not empty.
❓ Predict Output
advanced
2:00remaining
Output of array_column with associative arrays
What is the output of this PHP code?
PHP
<?php
$data = [
  ['id' => 1, 'name' => 'Alice'],
  ['id' => 2, 'name' => 'Bob'],
  ['id' => 3, 'name' => 'Charlie']
];
$names = array_column($data, 'name');
print_r($names);
?>
AArray ( [0] => Alice [1] => Bob [2] => Charlie )
BArray ( [1] => Alice [2] => Bob [3] => Charlie )
CSyntax error
DArray ( [id] => name )
Attempts:
2 left
💡 Hint
array_column extracts values from a specific key in each sub-array.
🧠 Conceptual
expert
2:00remaining
Why use array functions instead of loops?
Which of the following is the best reason to prefer PHP array functions like array_map, array_filter, and array_reduce over manual loops?
AThey allow modifying the original array without copying
BThey provide clearer, shorter code that expresses intent and reduces errors
CThey always run faster than loops in every situation
DThey automatically parallelize code for better performance
Attempts:
2 left
💡 Hint
Think about code readability and maintenance.

Practice

(1/5)
1. Why are PHP array functions important when working with lists?
easy
A. They make PHP run faster by changing the language core.
B. They simplify tasks like sorting and searching in arrays.
C. They allow arrays to store only numbers.
D. They prevent errors by disabling user input.

Solution

  1. Step 1: Understand the role of array functions

    Array functions provide ready-made tools to handle common tasks like sorting, searching, and counting elements in arrays.
  2. Step 2: Compare options

    Only They simplify tasks like sorting and searching in arrays. correctly states that array functions simplify tasks with arrays. Other options describe unrelated or incorrect effects.
  3. Final Answer:

    They simplify tasks like sorting and searching in arrays. -> Option B
  4. Quick Check:

    Array functions simplify array tasks = A [OK]
Hint: Remember: array functions make list tasks easier [OK]
Common Mistakes:
  • Thinking array functions change PHP core speed
  • Believing arrays can only hold numbers
  • Confusing array functions with input validation
2. Which of the following is the correct syntax to count elements in an array $arr in PHP?
easy
A. length($arr);
B. size($arr);
C. count($arr);
D. countElements($arr);

Solution

  1. Step 1: Recall PHP function for counting array elements

    The correct function to count elements in an array is count().
  2. Step 2: Check each option

    Only count($arr); uses count($arr); which is valid PHP syntax. Others are not valid PHP functions.
  3. Final Answer:

    count($arr); -> Option C
  4. Quick Check:

    Counting array elements uses count() = B [OK]
Hint: Use count() to get array size quickly [OK]
Common Mistakes:
  • Using length() which is not a PHP function
  • Trying size() which doesn't exist in PHP
  • Assuming countElements() is built-in
3. What will be the output of the following PHP code?
$numbers = [3, 1, 4, 1, 5];
sort($numbers);
print_r($numbers);
medium
A. [1, 1, 3, 4, 5]
B. Error: sort() is undefined
C. [3, 1, 4, 1, 5]
D. [5, 4, 3, 1, 1]

Solution

  1. Step 1: Understand what sort() does

    The sort() function sorts the array in ascending order, changing the original array.
  2. Step 2: Apply sort() to the array

    Sorting [3, 1, 4, 1, 5] ascending gives [1, 1, 3, 4, 5].
  3. Final Answer:

    [1, 1, 3, 4, 5] -> Option A
  4. Quick Check:

    sort() arranges numbers ascending = D [OK]
Hint: sort() arranges array ascending by default [OK]
Common Mistakes:
  • Thinking sort() sorts descending by default
  • Expecting original array unchanged
  • Assuming sort() is not a built-in function
4. Identify the error in this PHP code snippet:
$fruits = ['apple', 'banana', 'cherry'];
$found = in_array('banana', $fruits);
if ($found = true) {
    echo 'Found banana!';
}
medium
A. Array syntax is incorrect
B. in_array() is not a valid PHP function
C. Echo statement is missing a semicolon
D. Using '=' instead of '==' in the if condition

Solution

  1. Step 1: Check the if condition syntax

    The condition uses assignment '=' instead of comparison '==', so it always assigns true and evaluates as true.
  2. Step 2: Correct the condition

    It should be if ($found == true) or simply if ($found) to check the boolean value.
  3. Final Answer:

    Using '=' instead of '==' in the if condition -> Option D
  4. Quick Check:

    Use '==' to compare, not '=' [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
  • Confusing '=' with '==' in conditions
  • Thinking in_array() is invalid
  • Missing semicolon after echo (actually present)
5. You have an array of user ages: $ages = [21, 17, 30, 15, 25];.
Using PHP array functions, which code snippet correctly filters out ages less than 18 and returns the filtered array?
hard
A. $adults = array_filter($ages, fn($age) => $age >= 18);
B. $adults = array_map(fn($age) => $age >= 18, $ages);
C. $adults = array_reduce($ages, fn($carry, $age) => $age >= 18 ? $age : $carry);
D. $adults = array_search(fn($age) => $age >= 18, $ages);

Solution

  1. Step 1: Understand the goal

    We want to keep only ages 18 or older, so we need a function that filters elements based on a condition.
  2. Step 2: Identify correct array function

    array_filter() keeps elements where the callback returns true. $adults = array_filter($ages, fn($age) => $age >= 18); uses it correctly with a function checking age >= 18.
  3. Step 3: Check other options

    $adults = array_map(fn($age) => $age >= 18, $ages); uses array_map() which transforms elements but does not filter. $adults = array_reduce($ages, fn($carry, $age) => $age >= 18 ? $age : $carry); uses array_reduce() incorrectly for filtering. $adults = array_search(fn($age) => $age >= 18, $ages); uses array_search() wrongly with a callback.
  4. Final Answer:

    $adults = array_filter($ages, fn($age) => $age >= 18); -> Option A
  5. Quick Check:

    Use array_filter() to select elements by condition [OK]
Hint: Use array_filter() to keep elements matching condition [OK]
Common Mistakes:
  • Using array_map() instead of array_filter() for filtering
  • Misusing array_reduce() for filtering
  • Trying to use array_search() with a callback
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard