note 107666 added to function.array-fill
| From: | miguelxpain at gmail dot com | Date: | Fri, 24 Feb 2012 18:04:57 +0000 |
| Subject: | note 107666 added to function.array-fill | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186342@lists.php.net to get a copy of this message | ||
I made this function named "array_getMax" that returns te maximum value and index, from
array:
//using array_search_all by helenadeus at gmail dot com
function array_search_all($needle, $haystack)
{#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in
$haystack
foreach ($haystack as $k=>$v) {
if($haystack[$k]==$needle){
$array[] = $k;
}
}
return ($array);
}
function array_getMax($array){
$conteo=array_count_values($array);
if( count($conteo)==1 ){//returns full array when all values are the same.
return $array;
}
arsort($array);
//$antValue=null;
$maxValue=null;
$keyValue=null;
foreach($array as $key=>$value){
if($maxValue==null){
$maxValue=$value;
$keyValue=$key;
break;
}
}
$resultSearch=array_search_all($maxValue, $array);
return array_fill_keys($resultSearch, $maxValue);
}
//example
$arreglo=array('e1'=>99,'e2'=>'99','e3'=>1,'e4'=>1,'e5'=>98);
var_dump(array_getMax($arreglo));
//output
/*
array(2) {
["e1"]=>
int(99)
["e2"]=>
int(99)
}
*/
I hope some one find this usefull
----
Server IP: 148.206.80.9
Probable Submitter: 200.23.10.34
----
Manual Page -- http://www.php.net/manual/en/function.array-fill.php
Edit -- https://master.php.net/note/edit/107666
Del: integrated -- https://master.php.net/note/delete/107666/integrated
Del: useless -- https://master.php.net/note/delete/107666/useless
Del: bad code -- https://master.php.net/note/delete/107666/bad+code
Del: spam -- https://master.php.net/note/delete/107666/spam
Del: non-english -- https://master.php.net/note/delete/107666/non-english
Del: in docs -- https://master.php.net/note/delete/107666/in+docs
Del: other reasons-- https://master.php.net/note/delete/107666
Reject -- https://master.php.net/note/reject/107666
Search -- https://master.php.net/manage/user-notes.php