Here I could do it:
$file = fopen("words.xls","w+"); //clear the content every time before start writing. To see new content everytime
fclose($file);
for($i = 5; $i > 0 ; $i--)
{
$select_words = mysqli_query($con,"SELECT * from review_details where rate = $i and category = 'italian' and isApi = 0");
while ($row1 = @mysqli_fetch_array($select_words))
{
//echo $row1[review]."<br>";
// echo $row1[rate]."<br>";
$word.=$row1[adjective].",";
}
//echo "Words are : $word";
echo "<br/><br/><strong> $i star word count </strong>";
$word_count = array_count_values(str_word_count($word,1));
arsort($word_count);
echo "<br>";
echo "<br>";
//var_dump($word_count);
$word_count2 = array_slice($word_count,0,20);
print_r($word_count2);
unset($word);
unset($word_count);
unset($select_words);
file_put_contents('words.xls', print_r($word_count2, true), FILE_APPEND | LOCK_EX);
}