Challenge - 5 Problems
Associative Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); ?>
Attempts:
2 left
💡 Hint
Remember that numeric string keys are converted to integers in PHP arrays.
✗ Incorrect
In PHP, string keys that are numeric strings like "3" are converted to integer keys. So the key "3" becomes 3. The array has keys 'a', 2, 3, and 4 with their respective values.
❓ Predict Output
intermediate1: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); ?>
Attempts:
2 left
💡 Hint
Count returns the number of key-value pairs in an array.
✗ Incorrect
The array has three key-value pairs: 'x', 'y', and 'z'. So count returns 3.
🔧 Debug
advanced2: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); ?>
Attempts:
2 left
💡 Hint
Check the syntax for key-value pairs in arrays.
✗ Incorrect
The key 'key2' is missing the => operator before its value 200, causing a syntax error.
🧠 Conceptual
advanced1:30remaining
Associative array key type behavior
Which statement about PHP associative array keys is TRUE?
Attempts:
2 left
💡 Hint
Think about how PHP converts different types to keys internally.
✗ Incorrect
In PHP, boolean true is converted to integer 1 when used as an array key. Float keys are cast to integers. Null is cast to empty string. Arrays cannot be used as keys.
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
Look for a function that pairs keys and values into an associative array.
✗ Incorrect
array_combine creates an associative array by using one array for keys and another for values. array_merge merges arrays sequentially. array_map returns an array of arrays, not a single associative array. array_fill_keys fills all keys with the same value.