menu

PHP Arrays


1. Which function is used to sort an array in descending order by value in PHP?

sort()

asort()

ksort()

rsort()


2. Which function is used to remove the last element from an array in PHP?

array_shift()

array_pop()

array_remove_last()

remove_array_last()


3. Which function is used to sort an array in ascending order by value in PHP?

sort()

asort()

ksort()

rsort()


4. Which of the following is not a valid way to remove an element from an array in PHP?

unset()

array_splice()

array_remove()

array_shift()


5. What is an array in PHP?

A collection of similar data types

A collection of different data types

A variable that can hold only one value at a time

None of the above


6.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
$last_element = end($array);
echo $last_element;
?>

apple

banana

orange

0


7. Which of the following is not a valid way to loop through an array in PHP?

foreach()

for()

while()

do-while()


8.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
$subset = array_slice($array, 1, 2);
print_r($subset);
?>

["apple"]

["banana"]

["orange"]

["banana", "orange"]


9.

What is the output of the following PHP code snippet?

<?php
$array1 = ["apple", "banana"];
$array2 = ["orange", "grape"];
$result = array_merge($array1, $array2);
print_r($result);
?>

["apple", "banana"]

["orange", "grape"]

[0=>"apple", 1=>"banana", 2=>"orange", 3=>"grape"]

None of the above


10. How can you access the first element of an array in PHP?

$array[0]

$array[1]

$array[first]

$array["first"]