Sunday 15 July 2012

php - array_count_values randomise matching values -



php - array_count_values randomise matching values -

i know below code

<?php $array = array("1", "hello", "1", "world", "hello"); print_r(array_count_values($array)); ?>

will output:

array ( [1] => 2 [hello] => 2 [world] => 1 )

i retrieve value has been repeated most, or if there multiple matching instances have been repeated e.g "hello" , "1" randomise them instead of retrieving first one.

live: http://ideone.com/d1ydbd

$array = array(1, "hello", 1, "world", "hello",'2','2','2','3','3','3'); $s = array_count_values($array); $mostrepeated = max($s); $s = array_filter( $s, function($v) utilize ($mostrepeated) { homecoming $v==$mostrepeated; }); print_r($s); echo array_rand($s); // randomized

step:

find highest count remove elements less frequency return element random array_rand

note wrote function transparent whenever there single or multiple entries same value count.

php

No comments:

Post a Comment