Challenge - 5 Problems
Indexed Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code creating an indexed array?
Consider the following PHP code that creates an indexed array. What will be the output when it is run?
PHP
<?php $array = array('apple', 'banana', 'cherry'); print_r($array); ?>
Attempts:
2 left
💡 Hint
Remember that indexed arrays in PHP start with index 0 by default.
✗ Incorrect
In PHP, when you create an indexed array without specifying keys, the indexes start at 0 and increase by 1 for each element. So the keys are 0, 1, and 2 for the three elements.
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using short array syntax?
Look at this PHP code that uses the short array syntax. What will it output?
PHP
<?php $fruits = ['orange', 'grape', 'melon']; var_dump($fruits); ?>
Attempts:
2 left
💡 Hint
var_dump shows detailed info including indexes starting at zero.
✗ Incorrect
The short array syntax [] creates an indexed array starting at 0. var_dump shows the type, length, and keys with values. So keys are 0,1,2 with corresponding strings.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code mixing keys and values in an indexed array?
What will this PHP code output when run?
PHP
<?php $array = array(0 => 'zero', 2 => 'two', 'one'); print_r($array); ?>
Attempts:
2 left
💡 Hint
When mixing explicit keys and implicit values, PHP assigns the next index after the highest numeric key.
✗ Incorrect
The array has keys 0 and 2 explicitly. The next value 'one' has no key, so PHP assigns it key 3 (one more than highest key 2).
❓ Predict Output
advanced2:00remaining
What error or output does this PHP code produce?
Examine this PHP code snippet. What will happen when it runs?
PHP
<?php $array = array(1 => 'a', 3 => 'b', 2 => 'c'); print_r($array); ?>
Attempts:
2 left
💡 Hint
PHP arrays can have keys in any order; print_r shows them in insertion order.
✗ Incorrect
The array keys are explicitly set as 1, 3, and 2. PHP keeps keys as given. print_r outputs them in the order inserted.
🧠 Conceptual
expert2:00remaining
How many elements are in this PHP indexed array after execution?
After running this PHP code, how many elements does the array contain?
PHP
<?php $array = []; $array[] = 'first'; $array[5] = 'sixth'; $array[] = 'seventh'; ?>
Attempts:
2 left
💡 Hint
Appending without key uses the highest existing numeric key plus one.
✗ Incorrect
Initially empty array. First append adds element at index 0. Then index 5 is set. Next append adds element at index 6 (5 + 1). So total elements: 3.