note 107666 modified in function.array-fill by danbrown

From: Date: Thu, 10 May 2012 20:21:44 +0000
Subject: note 107666 modified in function.array-fill by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187787@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:

<?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 --was-- 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 http://php.net/manual/en/function.array-fill.php

« previous php.notes (#187787) next »