menu

PHP Arrays


1. 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


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

$array[0]

$array[1]

$array[first]

$array["first"]


3.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
echo $array[1];
?>

apple

banana

orange

None of the above


4.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
$reversed_array = array_reverse($array);
print_r($reversed_array);
?>

["orange", "banana", "apple"]

["apple", "banana", "orange"]

["orange", "apple", "banana"]

None of the above


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

sort()

asort()

ksort()

rsort()


6.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
$new_array = array_values($array);
print_r($new_array);
?>

["apple", "banana", "orange"]

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

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

None of the above


7.

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


8. What is the function used to add an element to the end of an array in PHP?

array_push()

array_add()

add_array()

array_append()


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

array_shift()

array_pop()

array_remove_last()

remove_array_last()


10.

What is the output of the following PHP code snippet?

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

0

1

2

3