Learn about PHP arrays and test your knowledge with these 20+ multiple-choice questions on array declaration, manipulation, and functions.
What is an array in PHP?
- A collection of variables
- A collection of functions
- A collection of objects
- A collection of constants
Answer: a
How do you declare an array in PHP?
- array()
- declare array()
- new array()
- make array()
Answer: a
What is the function to add an element to an array in PHP?
- push()
- add()
- append()
- array_push()
Answer: d
What is the function to remove the last element from an array in PHP?
- array_last()
- remove_last()
- pop()
- array_pop()
Answer: d
What is the function to remove an element from an array based on its key?
- remove_key()
- unset()
- array_remove()
- array_unset()
Answer: b
What is the function to check if a specific value exists in an array?
- in_array()
- array_contains()
- value_exists()
- array_search()
Answer: a
What is the function to sort an array in ascending order?
- sort()
- rsort()
- asort()
- arsort()
Answer: a
What is the function to merge two arrays together?
- concat()
- array_merge()
- merge()
- add_arrays()
Answer: b
What is the output of the following code?
$colors = array("red", "green", "blue");echo count($colors);
- 3
- 4
- 5
- Error
Answer: a
What is the output of the following code?
$numbers = array(2, 5, 1, 7, 3);sort($numbers);echo $numbers[0];
- 1
- 2
- 3
- 7
Answer: a
What is the difference between array_push() and array_merge() functions in PHP?
- array_push() is used to add elements to the end of an array, while array_merge() is used to merge two or more arrays together
- array_push() is used to merge two or more arrays together, while array_merge() is used to add elements to the end of an array
- Both functions are used to merge arrays together
- Both functions are used to add elements to the end of an array
Answer: a
What is the output of the following code?
$fruits = array("apple", "banana", "orange");$key = array_search("banana", $fruits);echo $key;
- 1
- 2
- "banana"
- NULL
Answer: a
What is the output of the following code?
$numbers = array(2, 5, 1, 7, 3);$reversed_numbers = array_reverse($numbers);echo $reversed_numbers[0];
- 3
- 7
- 1
- 2
Answer: b
What is the output of the following code?
$numbers = array(2, 5, 1, 7, 3);echo max($numbers);
- 7
- 1
- 5
- 2
Answer: a
What is the difference between array_key_exists() and in_array() functions in PHP?
- array_key_exists() checks if a given key exists in an array, while in_array() checks if a given value exists in an array
- array_key_exists() checks if a given value exists in an array, while in_array() checks if a given key exists in an array
- Both functions are used to check if a given key exists in an array
- Both functions are used to check if a given value exists in an array
Answer: a
What is the output of the following code?
$colors = array("red", "green", "blue");array_pop($colors);echo count($colors);
- 2
- 3
- 4
- Error
Answer: a
What is the output of the following code?
$numbers = array(2, 5, 1, 7, 3);shuffle($numbers);echo $numbers[0];
- 1
- 2
- 3
- 7
Answer: Any of the options
What is the output of the following code?
$fruits = array("apple", "banana", "orange");array_push($fruits, "grape");echo count($fruits);
- 3
- 4
- 5
- 6
Answer: d
What is the output of the following code?
$names = array("John", "Jane", "Mark");$last = array_pop($names);echo $last;
- John
- Jane
- Mark
- Error
Answer: c
What is the difference between array_merge() and array_combine() functions in PHP?
- array_merge() combines two or more arrays into a single array, while array_combine() combines two arrays into a single associative array
- array_merge() combines two arrays into a single associative array, while array_combine() combines two or more arrays into a single array
- Both functions are used to combine arrays into a single associative array
- Both functions are used to combine arrays into a single array
Answer: a
What is the output of the following code?
$fruits = array("apple", "banana", "orange");$chunked = array_chunk($fruits, 2);print_r($chunked);
- Array ( [0] => Array ( [0] => apple [1] => banana ) [1] => Array ( [0] => orange ) )
- Array ( [0] => Array ( [0] => apple ) [1] => Array ( [0] => banana [1] => orange ) )
- Array ( [0] => Array ( [0] => apple [1] => orange ) [1] => Array ( [0] => banana ) )
- Array ( [0] => Array ( [0] => apple [1] => banana [2] => orange ) )
Answer: a
What is the output of the following code?
$numbers = array(2, 5, 1, 7, 3);sort($numbers);echo $numbers[0];
- 1
- 2
- 3
- 7
Answer: a
What is the output of the following code?
$numbers = array(2, 5, 1, 7, 3);$sum = array_sum($numbers);echo $sum;
- 11
- 18
- 15
- 21
Answer: b