Voting

: seven minus two?
(Example: nine)

The Note You're Voting On

miguelxpain at gmail dot com
13 years ago
I made this function named "array_getMax" that returns te maximum value and index, from array:

<?php
//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

<< Back to user notes page

To Top