menu

PHP Arrays


1. Which of the following is the correct syntax to create an array in PHP?

$array = array("apple", "banana", "orange");

$array = ["apple", "banana", "orange"];

Both A and B

None of the above


2.

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


3.

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"]


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

$array[0]

$array[1]

$array[first]

$array["first"]


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

sort()

asort()

ksort()

rsort()


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

sort()

asort()

ksort()

rsort()


7. 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()


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

array_shift()

array_pop()

array_remove_last()

remove_array_last()


9.

What is the output of the following PHP code snippet?

<?php
$array = ["apple", "banana", "orange"];
$lengths = array_map('strlen', $array);
print_r($lengths);
?>

["a", "b", "o"]

[5, 6, 6]

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

None of the above


10. Which function is used to check if a value exists in an array in PHP?

in_array()

array_exists()

value_in_array()

array_contains()