Friday, August 28, 2015

findings frequency number from array using for loop

<?php
/**
 * Created by PhpStorm.
 * User: Polo-Dev
 * Date: 29-Aug-15
 * Time: 9:20 AM
 */
$string = "hi how are you hi you";
$array = explode(" ", $string);
$length = count($array);
$new_array = array();
for($i = 0; $i < $length; $i++){
    if(array_key_exists($array[$i], $new_array)){
        $new_array[$array[$i]] ++;
    }else{

        $new_array[$array[$i]] = 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; }