Write test and debug if necessary PHP scripts for the specif
Write, test, and debug (if necessary) PHP scripts for the specifications that follow.
9.3 Parameter: An array of strings.
Return Value: A list of the strings that occur most frequently in the parameter array.
Solution
function mostoccurences($strs){
/*Individual words will be stored here*/
$word = arr();
/*Multiple strings will be processed here*/
if(is_arr($strs))
foreach($strs as $str)
$word = arr_merge($word, explode(\" \", $str));
/*This is for preparing single string*/
else
$word = explode(\" \",$strs);
/*arr is used to count words*/
$index = arr();
/*Aggregate word counters*/
foreach($word as $word)
/*Increment count or create if it doesn\'t exist*/
(isset($index[$word]))? $index[$word]++ : $index[$word] = 1;
/*Sort arr hy highest value and */
arsort($index);
/*Return the word*/
return key($index);
}
if you like this answer, please give a thumbs up and if you have some doubt just ask in the comment section below. I will try to help. Cheers
