Bird
Raised Fist0
PHPprogramming~20 mins

Associative array creation in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Associative Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of associative array creation with mixed keys
What is the output of the following PHP code?
PHP
<?php
$arr = ["a" => 1, 2 => "b", "3" => "c", 4 => "d"];
print_r($arr);
?>
AArray ( [0] => a [1] => 1 [2] => b [3] => c [4] => d )
BArray ( [a] => 1 [2] => b [3] => c [4] => d [0] => 0 )
CArray ( [a] => 1 [2] => b [3] => c [4] => d )
DArray ( [a] => 1 [2] => b [3] => c )
Attempts:
2 left
💡 Hint
Remember that numeric string keys are converted to integers in PHP arrays.
❓ Predict Output
intermediate
1:30remaining
Count items in associative array created with array()
How many items does the following associative array contain?
PHP
<?php
$assoc = array('x' => 10, 'y' => 20, 'z' => 30);
echo count($assoc);
?>
AError
B0
C1
D3
Attempts:
2 left
💡 Hint
Count returns the number of key-value pairs in an array.
🔧 Debug
advanced
2:00remaining
Identify the error in associative array creation
What error does the following PHP code produce?
PHP
<?php
$arr = ["key1" => 100, "key2" 200, "key3" => 300];
print_r($arr);
?>
AParse error: syntax error, unexpected '200' (missing =>)
BNotice: Undefined index 'key2'
CWarning: Array to string conversion
DNo error, outputs the array
Attempts:
2 left
💡 Hint
Check the syntax for key-value pairs in arrays.
🧠 Conceptual
advanced
1:30remaining
Associative array key type behavior
Which statement about PHP associative array keys is TRUE?
ANull keys are converted to string 'null'
BBoolean true is converted to integer 1 as a key
CArrays can be used as keys
DFloat keys are stored as floats
Attempts:
2 left
💡 Hint
Think about how PHP converts different types to keys internally.
🚀 Application
expert
2:30remaining
Create associative array from two lists
Given two arrays $keys = ['name', 'age', 'city'] and $values = ['Alice', 30, 'Paris'], which code correctly creates an associative array combining keys and values?
A$assoc = array_combine($keys, $values);
B$assoc = array_fill_keys($keys, $values);
C$assoc = array_map(fn($k, $v) => [$k => $v], $keys, $values);
D$assoc = array_merge($keys, $values);
Attempts:
2 left
💡 Hint
Look for a function that pairs keys and values into an associative array.

Practice

(1/5)
1. Which of the following is the correct way to create an associative array in PHP with keys "name" and "age"?
easy
A. $person = ("name" => "Alice", "age" => 30);
B. $person = {"name": "Alice", "age": 30};
C. $person = array("Alice", 30);
D. $person = ["name" => "Alice", "age" => 30];

Solution

  1. Step 1: Understand PHP associative array syntax

    Associative arrays use square brackets with key => value pairs.
  2. Step 2: Identify correct syntax among options

    $person = ["name" => "Alice", "age" => 30]; uses square brackets and => correctly; others use invalid syntax.
  3. Final Answer:

    $person = ["name" => "Alice", "age" => 30]; -> Option D
  4. Quick Check:

    Associative array = key => value pairs [OK]
Hint: Use square brackets and => for associative arrays [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Using colon (:) instead of =>
  • Creating indexed arrays instead of associative
2. Which of the following PHP code snippets will cause a syntax error when creating an associative array?
easy
A. $data = ["key1" : "value1", "key2" : "value2"];
B. $data = array("key1" => "value1", "key2" => "value2");
C. $data = ["key1" => "value1", "key2" => "value2"];
D. $data = ['key1' => 'value1', 'key2' => 'value2'];

Solution

  1. Step 1: Check the syntax for key-value pairs

    PHP uses => to assign values to keys, not colon (:).
  2. Step 2: Identify the invalid syntax

    $data = ["key1" : "value1", "key2" : "value2"]; uses colon (:) which is invalid in PHP associative arrays.
  3. Final Answer:

    $data = ["key1" : "value1", "key2" : "value2"]; -> Option A
  4. Quick Check:

    Use => not : for keys in PHP arrays [OK]
Hint: Remember PHP uses =>, not : for associative arrays [OK]
Common Mistakes:
  • Using colon (:) instead of =>
  • Mixing array() and [] syntax incorrectly
  • Missing commas between pairs
3. What will be the output of the following PHP code?
$arr = ["fruit" => "apple", "color" => "red"];
echo $arr["color"];
medium
A. red
B. color
C. apple
D. fruit

Solution

  1. Step 1: Understand array key access

    Accessing $arr["color"] returns the value for key "color".
  2. Step 2: Identify the value for key "color"

    Value is "red", so echo outputs "red".
  3. Final Answer:

    red -> Option A
  4. Quick Check:

    Key "color" = "red" [OK]
Hint: Access values by keys with $array["key"] [OK]
Common Mistakes:
  • Confusing keys and values
  • Using wrong quotes or missing quotes
  • Trying to access with dot notation
4. Identify the error in the following PHP code that tries to create an associative array:
$info = ["name" => "John", "age" = 25];
medium
A. Missing comma between elements
B. Keys should not be in quotes
C. Using = instead of => for key-value pair
D. Array must use array() syntax

Solution

  1. Step 1: Check key-value assignment syntax

    PHP requires => to assign values to keys, not =.
  2. Step 2: Locate incorrect assignment

    "age" = 25 uses = instead of =>, causing syntax error.
  3. Final Answer:

    Using = instead of => for key-value pair -> Option C
  4. Quick Check:

    Use => for key-value pairs [OK]
Hint: Use =>, not =, between key and value [OK]
Common Mistakes:
  • Using single = instead of =>
  • Omitting commas between pairs
  • Not quoting string keys
5. You want to create an associative array in PHP that maps product names to their prices, but only include products with a price greater than 20. Which code correctly creates this filtered associative array?
hard
A. $products = ["apple" => 15, "banana" => 25, "cherry" => 30]; $filtered = []; foreach ($products as $name, $price) { if ($price > 20) { $filtered[$name] = $price; } }
B. $products = ["apple" => 15, "banana" => 25, "cherry" => 30]; $filtered = []; foreach ($products as $name => $price) { if ($price > 20) { $filtered[$name] = $price; } }
C. $products = ["apple" => 15, "banana" => 25, "cherry" => 30]; $filtered = array_filter($products, function($name, $price) { return $price > 20; });
D. $products = ["apple" => 15, "banana" => 25, "cherry" => 30]; $filtered = []; foreach ($products as $price => $name) { if ($price > 20) { $filtered[$name] = $price; } }

Solution

  1. Step 1: Understand foreach key-value syntax

    Use foreach ($array as $key => $value) to access keys and values correctly.
  2. Step 2: Filter products with price > 20

    $products = ["apple" => 15, "banana" => 25, "cherry" => 30]; $filtered = []; foreach ($products as $name => $price) { if ($price > 20) { $filtered[$name] = $price; } } correctly checks price and assigns filtered items to new array.
  3. Final Answer:

    The code using foreach ($products as $name => $price) -> Option B
  4. Quick Check:

    foreach ($arr as $key => $value) for associative arrays [OK]
Hint: Use foreach ($arr as $key => $value) to access associative arrays [OK]
Common Mistakes:
  • Swapping key and value variables in foreach
  • Using incorrect foreach syntax with two variables separated by comma
  • Misusing array_filter with wrong callback parameters
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