0
0
PHPprogramming~20 mins

Indexed array creation in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Indexed Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
?>
AArray ( apple, banana, cherry )
BArray ( [0] => apple [1] => banana [2] => cherry )
CArray ( [0] => apple [2] => banana [4] => cherry )
DArray ( [1] => apple [2] => banana [3] => cherry )
Attempts:
2 left
💡 Hint
Remember that indexed arrays in PHP start with index 0 by default.
Predict Output
intermediate
2: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);
?>
Aarray(3) { [0]=> string(6) "orange" [1]=> string(5) "grape" [2]=> string(5) "melon" }
BArray ( [1] => orange [2] => grape [3] => melon )
Carray(3) { [1]=> string(6) "orange" [2]=> string(5) "grape" [3]=> string(5) "melon" }
DArray ( orange, grape, melon )
Attempts:
2 left
💡 Hint
var_dump shows detailed info including indexes starting at zero.
Predict Output
advanced
2: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);
?>
AArray ( [0] => zero [1] => one [2] => two )
BArray ( [0] => zero [1] => two [2] => one )
CArray ( [0] => zero [2] => two [3] => one )
DArray ( [0] => zero [2] => two [1] => one )
Attempts:
2 left
💡 Hint
When mixing explicit keys and implicit values, PHP assigns the next index after the highest numeric key.
Predict Output
advanced
2: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);
?>
AArray ( [1] => a [3] => b [2] => c )
BArray ( [0] => a [1] => b [2] => c )
CSyntaxError: Unexpected token
DArray ( [1] => a [2] => c [3] => b )
Attempts:
2 left
💡 Hint
PHP arrays can have keys in any order; print_r shows them in insertion order.
🧠 Conceptual
expert
2: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';
?>
A2
B5
C4
D3
Attempts:
2 left
💡 Hint
Appending without key uses the highest existing numeric key plus one.