menu

PHP Arrays


1.

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


2.

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


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

in_array()

array_exists()

value_in_array()

array_contains()


4.

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


5.

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


6.

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


7.

What is the output of the following PHP code snippet?

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

["apple", "banana"]

["orange", "grape"]

[]

None of the above


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

$array[0]

$array[1]

$array[first]

$array["first"]


9.

What is the output of the following PHP code snippet?

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

0

1

2

3


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