Q
What will be the output of the following PHP code?
<?php
$matrix = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
echo $matrix[1][2];
?

Answer & Solution

Answer: Option D
Solution:
The output of the code will be 6 because multidimensional arrays in PHP use two sets of indices to access elements: the first index represents the row number, and the second index represents the column number. In this case, $matrix[1][2] refers to the element in the second row (index 1) and the third column (index 2), which is 6.
Related Questions on Average

What type of index do indexed arrays in PHP use?

A). String

B). Integer

C). Key-value pairs

D). Floating-point number

What is the correct syntax for creating a multidimensional array in PHP?

A). $array = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9));

B). $array = array([1, 2, 3], [4, 5, 6], [7, 8, 9]);

C). $array = array{1, 2, 3}, {4, 5, 6}, {7, 8, 9};

D). $array = array{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Which PHP function is used to determine the number of elements in an array?

A). size()

B). count()

C). length()

D). elements()

Which PHP function is used to extract a slice of an array?

A). splice()

B). slice()

C). extract()

D). split()

What will be the output of the following PHP code?
<?php
$arr = array(10, 20, 30, 40, 50);
$slice = array_slice($arr, 2);
foreach ($slice as $item) {
echo $item . ' ';
}
?

A). 10 20 30 40 50

B). 10 20

C). 30 40 50

D). 30 40 50

Which PHP function is used to merge two or more arrays into a single array?

A). merge()

B). combine()

C). concat()

D). array_merge()

What will be the output of the following PHP code?
<?php
$colors = array('Red' => 1, 'Green' => 2, 'Blue' => 3);
asort($colors);
foreach ($colors as $color => $value) {
echo '$color: $value ';
}
?

A). Red: 1 Green: 2 Blue: 3

B). 1: Red 2: Green 3: Blue

C). 1: Blue 2: Green 3: Red

D). Red: 3 Green: 2 Blue: 1

What will be the output of the following PHP code?
<?php
$person = array('name' => 'John', 'age' => 30, 'city' => 'New York');
echo $person['age'];
?

A). name: John

B). age: 30

C). city: New York

D). John

What is the correct way to access the first element of an indexed array in PHP?

A). $array[0]

B). $array[1]

C). $array['first']

D). $array['0']

What is the output of the following PHP code?
<?php
$colors = array('Red', 'Green', 'Blue');
echo $colors[1];
?

A). Red

B). Green

C). Blue

D). 1