Array Questions
Array Questions
Solve following question
$color = array('white', 'green', 'red', 'blue', 'black',’sky’);
Write a script which will display the following string
Expected Output:
"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', ‘sky’ and 'white' will come from $color.
2. Write a PHP script which will display the colors in the following way :
$color = array('white', 'green', 'red'')
Expected Output :
a) white, green, red,
b)
•green
•red
•white
3. Create a PHP script which displays the capital and country name from the below array
$list.
$list = array( "Nepal"=>"Kathmandu", "India"=>"New Delhi", "Australia"=> "Sydney",
"Denmark"=>"Copenhagen", "Finland"=>"Helsinki", "France" => "Paris",
"Slovakia"=>"Bratislava", "Netherlands"=>"Amsterdam", "Portugal"=>"Lisbon",
"Spain"=>"Madrid") ;
Sort the list by the capital of the country.
Sample Output :
The capital of Nepal is Kathmandu
The capital of India is New Delhi
The capital of Australia is Sydney
…………. and so on
4. $x = array(1, 2, 3, 4, 5);
Delete an element from the above PHP array. After deleting the element, integer keys must
be normalized.
Expected Output :
First : array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }
After Delete : array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(5) }
5. Write a PHP script that inserts a new item in an array in any position.
a)Using array_splice and using other function.
b) With out using array_splice
Expected Output :
Original array : 1 2 3 4 5
After inserting '$' the array is : 1 2 3 $ 4 5
6. Write a PHP script to sort the following associative array
array("Ram"=>"31","Hari"=>"41","Sita"=>"39","Gita"=>"40") in
a) ascending order sort by value
b) ascending order sort by Key
c) descending order sorting by Value
d) descending order sorting by Key
7. Write a PHP script to calculate and display average weight, five lowest and highest
weights.
Recorded weights : $temp =
“50,60,70,90,42,62,52,87,32,45,75,25,60,61,66,65,57,65,32,45,75,45,45,55,56,57,58,59,6
0,61,62,63,64”
8. Write a PHP function to change the following array's all values to upper or lower case.
Sample arrays:
$brand = array('A' => 'Apple', 'B' => 'Bird', 'c' => 'Carbon');
9. Write a PHP script to generate unique random numbers within a range.
Sample Range: (11, 20)
Sample Output: 17 16 13 20 14 19 18 15 11 12