0
0
PHPprogramming~10 mins

Associative array creation in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an associative array with keys 'name' and 'age'.

PHP
$person = array('name' => 'John', 'age' => [1]);
Drag options to blanks, or click blank then click option'
AJohn
B'30'
C30
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the number, making it a string.
Using a variable name instead of a value.
2fill in blank
medium

Complete the code to add a new key 'city' with value 'Paris' to the associative array.

PHP
$person['city'] = [1];
Drag options to blanks, or click blank then click option'
A'Paris'
BParis
C"Paris"
Dcity
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the string value.
Using the key name as the value.
3fill in blank
hard

Fix the error in the associative array creation by completing the code.

PHP
$data = array([1] => 'apple', 'b' => 'banana');
Drag options to blanks, or click blank then click option'
A'a'
Ba
C1
D"a"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings as keys.
Using numbers when a string key is intended.
4fill in blank
hard

Fill both blanks to create an associative array with keys 'x' and 'y' and values 10 and 20.

PHP
$coords = array([1] => [2], 'y' => 20);
Drag options to blanks, or click blank then click option'
A'x'
B10
C'10'
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys.
Putting quotes around numeric values.
5fill in blank
hard

Fill all three blanks to create an associative array with keys 'first', 'second', 'third' and values 1, 2, 3.

PHP
$numbers = array([1] => [2], [3] => 3);
Drag options to blanks, or click blank then click option'
A'first'
B1
C'second'
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting string keys.
Quoting numeric values.