0
0
PHPprogramming~20 mins

Array key and value extraction in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Key and Value Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of array_keys with numeric keys
What is the output of the following PHP code?
PHP
<?php
$array = [10 => 'a', 20 => 'b', 30 => 'c'];
print_r(array_keys($array));
?>
AArray ( [0] => 10 [1] => 20 [2] => 30 )
BArray ( [0] => a [1] => b [2] => c )
CArray ( [10] => 0 [20] => 1 [30] => 2 )
DArray ( [0] => 0 [1] => 1 [2] => 2 )
Attempts:
2 left
💡 Hint
array_keys returns the keys of the array, not the values.
Predict Output
intermediate
2:00remaining
Output of array_values with associative keys
What will this PHP code output?
PHP
<?php
$array = ['x' => 100, 'y' => 200, 'z' => 300];
print_r(array_values($array));
?>
AArray ( [0] => 100 [1] => 200 [2] => 300 )
BArray ( [0] => x [1] => y [2] => z )
CArray ( [x] => 100 [y] => 200 [z] => 300 )
DArray ( [100] => 0 [200] => 1 [300] => 2 )
Attempts:
2 left
💡 Hint
array_values returns the values of the array, ignoring keys.
Predict Output
advanced
2:00remaining
Result of array_keys with search value
What does this PHP code output?
PHP
<?php
$array = ['a' => 1, 'b' => 2, 'c' => 1, 'd' => 3];
print_r(array_keys($array, 1));
?>
AArray ( )
BArray ( [0] => a [1] => c )
CArray ( [a] => 1 [c] => 1 )
DArray ( [0] => 1 [1] => 1 )
Attempts:
2 left
💡 Hint
array_keys can accept a second parameter to filter keys by value.
Predict Output
advanced
2:00remaining
Output of array_values on mixed keys
What is the output of this PHP code?
PHP
<?php
$array = [5 => 'apple', 'color' => 'red', 10 => 'banana'];
print_r(array_values($array));
?>
AArray ( [apple] => 0 [red] => 1 [banana] => 2 )
BArray ( [5] => apple [color] => red [10] => banana )
CArray ( [0] => 5 [1] => color [2] => 10 )
DArray ( [0] => apple [1] => red [2] => banana )
Attempts:
2 left
💡 Hint
array_values returns values reindexed with numeric keys starting at 0.
Predict Output
expert
2:00remaining
Count of keys returned by array_keys with strict search
What is the output of this PHP code?
PHP
<?php
$array = [0 => '0', 1 => 0, 2 => false, 3 => null];
$result = array_keys($array, 0, true);
echo count($result);
?>
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
The third parameter in array_keys enforces strict type comparison.