Arrays - Study Mode
[#46] What will be the output of the following PHP code? <?php $a1 = array( "red" , "green" )
$a2 = array( "blue" , "yellow" )
print_r(array_merge( $a1, $a2 ))
?>
Correct Answer
(C) Array ( [0] => red [1] => green [2] => blue [3] => yellow )
Explanation
Solution: The array_merge() function merges one or more arrays into one array.
[#47] What will be the output of the following PHP code? <?php $a = array( "a" => "red" , "b" => "green" , "c" => "blue" )
echo array_shift( $a )
print_r ( $a )
?>
Correct Answer
(B) red
Explanation
Solution: The array_shift() function removes the first element from an array, and returns the value of the removed element.
[#48] What will be the output of the following PHP code? <?php $a = array( "red" , "green" , "blue" )
array_pop( $a )
print_r( $a )
?>
Correct Answer
(A) Array ( [0] => red [1] => green )
Explanation
Solution: The array_pop() function deletes the last element of an array.
[#49] What will be the output of the following PHP code? <?php $fruits = array ( "mango" , "apple" , "pear" , "peach" )
$fruits = array_flip( $fruits )
echo ( $fruits [ 0 ])
?>
Correct Answer
(B) error
Explanation
Solution: As we are flipping the values, $fruits[“mango”] = 0, $fruits[“apple”] = 1 and so on.
[#50] What will be the output of the following PHP code? <?php $fruits = array ( "mango" , "apple" , "peach" , "pear" )
$fruits = asort ( $fruits )
printr ( $fruits )
?>
Correct Answer
(A) Array ( [1] => apple [0] => mango [2] => peach [3] => pear )
Explanation
Solution: The function asort() sorts the array in ascending order, except that the key/value corresponding is maintained.