Bird
Raised Fist0
PHPprogramming~20 mins

Indexed array creation in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following correctly creates an indexed array with three elements in PHP?
easy
A. $arr = ["apple", "banana", "cherry"];
B. $arr = {"apple", "banana", "cherry"};
C. $arr = ("apple", "banana", "cherry");
D. $arr = array:"apple", "banana", "cherry";

Solution

  1. Step 1: Recognize PHP array syntax

    PHP indexed arrays can be created using square brackets [] or the array() function.
  2. Step 2: Check each option's syntax

    $arr = ["apple", "banana", "cherry"]; uses [] with comma-separated values, which is correct. Options A, B, and C use invalid syntax for arrays.
  3. Final Answer:

    $arr = ["apple", "banana", "cherry"]; -> Option A
  4. Quick Check:

    Correct array syntax = $arr = ["apple", "banana", "cherry"]; [OK]
Hint: Use square brackets [] or array() to create indexed arrays [OK]
Common Mistakes:
  • Using curly braces {} instead of []
  • Using parentheses () without array()
  • Incorrect use of colons or other symbols
2. Which of the following is the correct syntax to create an indexed array with the elements 10, 20, and 30 in PHP?
easy
A. $numbers = [10; 20; 30];
B. $numbers = array(10, 20, 30);
C. $numbers = (10, 20, 30);
D. $numbers = {10, 20, 30};

Solution

  1. Step 1: Recall PHP array creation syntax

    PHP allows creating arrays using array() with parentheses and comma-separated values.
  2. Step 2: Validate each option

    $numbers = array(10, 20, 30); uses array() correctly. Options B, C, and D use invalid separators or brackets.
  3. Final Answer:

    $numbers = array(10, 20, 30); -> Option B
  4. Quick Check:

    array() with commas = $numbers = array(10, 20, 30); [OK]
Hint: Use commas inside array() or [] to separate elements [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Using parentheses without array()
  • Using curly braces for arrays
3. What will be the output of the following PHP code?
$fruits = ["apple", "banana", "cherry"];
echo $fruits[1];
medium
A. banana
B. apple
C. cherry
D. Error

Solution

  1. Step 1: Understand array indexing

    Indexed arrays start at index 0, so $fruits[0] = "apple", $fruits[1] = "banana".
  2. Step 2: Identify the echoed element

    The code echoes $fruits[1], which is "banana".
  3. Final Answer:

    banana -> Option A
  4. Quick Check:

    Index 1 element = banana [OK]
Hint: Remember PHP arrays start at index 0 [OK]
Common Mistakes:
  • Confusing index 1 with index 0
  • Expecting output of entire array
  • Syntax errors from missing semicolons
4. Identify the error in the following PHP code that tries to create an indexed array:
$colors = array["red", "green", "blue"];
medium
A. Array elements must be integers
B. Missing semicolon at the end
C. Using double quotes instead of single quotes
D. Using square brackets [] instead of parentheses () with array

Solution

  1. Step 1: Check array() function syntax

    The array() function requires parentheses (), not square brackets [].
  2. Step 2: Identify the syntax error

    The code uses array[ ... ] which is invalid syntax and causes an error.
  3. Final Answer:

    Using square brackets [] instead of parentheses () with array -> Option D
  4. Quick Check:

    array() needs () not [] [OK]
Hint: Use parentheses () with array(), not square brackets [] [OK]
Common Mistakes:
  • Confusing array() with [] syntax
  • Forgetting semicolon (though present here)
  • Thinking quotes type matters for array creation
5. You want to create an indexed array of numbers from 1 to 5 in PHP without manually listing each number. Which code correctly does this?
hard
A. $numbers = array(1 to 5);
B. $numbers = [1..5];
C. $numbers = range(1, 5);
D. $numbers = {1, 2, 3, 4, 5};

Solution

  1. Step 1: Recall PHP function for range creation

    PHP provides the range() function to create arrays with sequences of numbers.
  2. Step 2: Evaluate each option

    $numbers = range(1, 5); uses range(1, 5) correctly. Options A, B, and D use invalid syntax or unsupported constructs.
  3. Final Answer:

    $numbers = range(1, 5); -> Option C
  4. Quick Check:

    Use range() for sequences [OK]
Hint: Use range(start, end) to create number arrays quickly [OK]
Common Mistakes:
  • Using invalid range syntax like [1..5]
  • Trying to write '1 to 5' inside array()
  • Using curly braces {} for arrays
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