menu

PHP Arrays

PHP Arrays - Important Points


16.

What is the output of the following PHP code snippet?

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

A. ["apple", "banana", "orange"]

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

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

D. None of the above

Discuss Work Space

Answer: option b

Explanation:

The array_values() function returns all the values of an array as a new indexed array, in this case resulting in [0 => "apple", 1 => "banana", 2 => "orange"].

17.

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);
?>

A. ["apple", "banana"]

B. ["orange", "grape"]

C. []

D. None of the above

Discuss Work Space

Answer: option c

Explanation:

The array_intersect() function returns an array containing all the values that are present in both of the input arrays, in this case resulting in an empty array because there are no common elements between $array1 and $array2.

18.

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);
?>

A. ["apple", "banana"]

B. ["orange", "grape"]

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

D. None of the above

Discuss Work Space

Answer: option c

Explanation:

The array_merge() function merges one or more arrays into a single array, in this case resulting in [0=>"apple", 1=>"banana", 2=>"orange", 3=>"grape"]

19.

What is the output of the following PHP code snippet?

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

A. apple

B. banana

C. orange

D. 0

Discuss Work Space

Answer: option c

Explanation:

The end() function moves the internal pointer of an array to its last element and returns that element, in this case resulting in "orange".


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now