arrays unit 3
arrays unit 3
Output :
white, green, red,
"The memory of that scene for me is like a frame of film forever frozen at that
moment: the red carpet, the green lawn, the white house, the leaden sky. The new
president and his first lady. - Richard M. Nixon"
and the words 'red', 'green' and 'white' will come from $color.
Associative Array
<?php
$age=array("bill"=>25, "john"=>28, "deep"=>27);
var_dump($age); Shows the data types of values
echo $age["john"]."<br>";
echo $age["bill"];
echo $age["deep"];
?>
TRY THIS
To loop through and print all the values of an associative array, you could use a foreach loop, like this:
<?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe
"=>"43");
If you want to delete the whole array you need to use unset($arr);
Sort Array in Ascending Order - sort()
The following example sorts the elements of the $cars array in ascending alphabetical order:
Sort Array in Descending Order - rsort()
The array_merge() function merges one or more arrays into one array.
Traversing Arrays
Traverse an array means to access each element (item) stored in
the array so that the data can be checked or used as part of a
process.
A) array_merge()
B) array_combine()
C) array_union()
D) array_join()
Which PHP function is used to combine two or more arrays into a single array?
A) array_merge()
B) array_combine()
C) array_union()
D) array_join()
Which function is used to find the number of elements in an array in PHP?
A) count()
B) length()
C) size()
D) sizeof()
Which function is used to find the number of elements in an array in PHP?
A) count()
B) length()
C) size()
D) sizeof()
In PHP, how do you check if a specific element exists in an array?
A) element_exists()
B) in_array()
C) array_search()
D) find_element()
In PHP, how do you check if a specific element exists in an array?
A) element_exists()
B) in_array()
C) array_search()
D) find_element()
Which method is used to remove an element
from the beginning of an array in PHP?
A) array_pop()
B) array_shift()
C) array_slice()
D) array_splice()
Which method is used to remove an element
from the beginning of an array in PHP?
A) array_pop()
B) array_shift()
C) array_slice()
D) array_splice()
Which PHP function is used to remove duplicate values from an array?
A) array_unique()
B) array_distinct()
C) array_remove_duplicates()
D) array_filter()
Which PHP function is used to remove duplicate values from an array?
A) array_unique()
B) array_distinct()
C) array_remove_duplicates()
D) array_filter()
What does the array_keys() function in PHP return?
A) array_extract()
B) array_slice()
C) array_subarray()
D) array_part()
Which array function is used to extract a portion of an array?
A) array_extract()
B) array_slice()
C) array_subarray()
D) array_part()
5. Which in-built function will add a value to the end
of an array?
A. array_unshift()
B. into_array()
C. inend_array()
D. array_push()
A. array_unshift()
B. into_array()
C. inend_array()
D. array_push()
4. Which of the following are correct
ways of creating an array?
i) arr[0] = "letsfindcourse";
ii) $arr[] = array("letsfindcourse");
iii) $arr[0] = "letsfindcourse";
iv) $arr = array("letsfindcourse");
i) arr[0] = "letsfindcourse";
ii) $arr[] = array("letsfindcourse");
iii) $arr[0] = "letsfindcourse";
iv) $arr = array("letsfindcourse");
6. Which function returns an array consisting
of associative key/value pairs?
A. count()
B. array_count()
C. array_count_values()
D. count_values()
Ans : C
Explanation: The function array_count_values() will count all the values of an
array.
8. As compared to associative arrays
vector arrays are much
A. Faster
B. Slower
C. Stable
D. None of the above
Ans : A
Explanation: As compared to associative arrays vector arrays
are much Faster.
11. Keys in array can be non-
sequential.
A. True
B. False
C. Depend on program
D. Keys does not exist in array
Ans : A
14. What is the use of array_unshift()
function?
A. Function is used to print the array in readable
format.
B. Adds elements to the beginning of the array and
returns the size of array.
C. Function can be used to verify if a variable is an
array. Returns TRUE or FALSE
D. Adds elements to the end of the array and returns
the size of array.
Ans : B
Explanation: array_unshift() Adds elements to
the beginning of the array and returns the size
of array.