Friday, August 28, 2015

findings frequency number from array using foreach loop

<?php
/**
 * Created by PhpStorm.
 * User: Polo-Dev
 * Date: 29-Aug-15
 * Time: 9:20 AM
 */
$string = "hi how are you hi you";
$words = explode(" ", $string);
$new_array = array();

foreach ($words as $word) {
   if(array_key_exists($word, $new_array)){
       $new_array[$word] ++;
   } else {
       $new_array[$word] = 1;
   }
}

foreach($new_array as $key => $value){
    echo "$key => $value <br>";
}

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }