Arrays - Study Mode
[#41] What will be the output of the following PHP code? <?php $array1 = array ( "KA" , "LA" , "CA" , "MA" , "TA" )
$array2 = array ( "KA" , "IA" , "CA" , "GA" , "TA" )
$inter = array_intersect ( $array1, $array2 )
print_r ( $inter )
?>
Correct Answer
(B) Array ( [0] => KA [2] => CA [4] => TA )
Explanation
Solution: The array_intersect() function returns a key preserved array consisting only of those values present in the first array that are also present in each of the other input arrays.
[#42] What will the following script output? <?php $array = array (1, 2, 3, 5, 8, 13, 21, 34, 55)
$sum = 0
for ($i = 0
$i < 5
$i++) { $sum += $array[$array[$i]]
} echo $sum
?>
Correct Answer
(A) 78
[#43] What elements will the following script output? <?php $array = array ( true => 'a', 1 => 'b' )
var_dump ( $array )
?>
Correct Answer
1 => 'b'
[#44] What will be the output of the following PHP code? <?php $a = array( "A" , "Cat" , "Dog" , "A" , "Dog" )
print_r(array_count_values( $a ))
?>
Correct Answer
(A) Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
Explanation
Solution: The array_count_values() function counts all the values of an array.
[#45] What will be the output of the following PHP code? <?php $a1 = array( "a" => "red" , "b" => "green" , "c" => "blue" , "d" => "yellow" )
$a2 = array( "e" => "red" , "f" => "green" , "g" => "blue" )
$result = array_diff( $a1, $a2 )
print_r( $result )
?>
Correct Answer
(A) Array ( [d] => yellow )
Explanation
Solution: The array_diff() function compares the values of two (or more) arrays, and returns the differences.