PHP arrays - Exercises, Practice, Solution

Last update on August 27 2018 07:02:02 (UTC/GMT +8 hours)

PHP arrays [59 exercises with solution]

1. $color = array('white', 'green', 'red', 'blue', 'black');
Write a script which will display the following string - Go to the editor
"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.

8. Write a PHP script to sort the following associative array : Go to the editor
array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"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

10. Write a PHP program to sort an array of positive integers using the Bead-Sort Algorithm. Go to the editor
According to Wikipedia "Bead-sort is a natural sorting algorithm, developed by Joshua J. Arulanandham, Cristian S. Calude and Michael J. Dinneen in 2002. Both digital and analog hardware implementations of bead sort can achieve a sorting time of O(n); however, the implementation of this algorithm tends to be significantly slower in software and can only be used to sort lists of positive integers".

13. Write a PHP script which displays all the numbers between 200 and 250 that are divisible by 4. Go to the editor
Note : Do not use any PHP control statement. Expected Output : 200,204,208,212,216,220,224,228,232,236,240,244,248

14. Write a PHP script to get the shortest/longest string length from an array. Go to the editorSample arrays : ("abcd","abc","de","hjjj","g","wer")Expected Output : The shortest array length is 1. The longest array length is 4.