100, "2010-05-04" =>400, "2008-05-01" =>800, "2011...">Geek asks and answersHow to find the maximum and minimum date by keyI have an array$a = array( "2010-05-03" =>100, "2010-05-04" =>400, "2008-05-01" =>800, "2011-01-01" =>800 ); How to find the maximum and minimum on key( date)?For instance:max => 2011-01-01 min => 2008-05-01 +3arrays phpkn3l May 04 '11 at 19:38source share5 answersI would be lazy and just let PHP look twice over the array. To find the minimum and the second time, to find the first matching key for this value:$min_key = array_search(min($a), $a); Or for maximum:$max_key = array_search(max($a), $a); +8mario May 04 '11 at 19:41source share(Not sure if you want the values for the max and min keys or the keys for the max and min values.)This returns the values for the max / min keys:$keys = array_keys($a); $min = $a[min($keys)); $max = $a[max($keys)); +4Philippe gerber May 04 '11 at 19:47source share$maxK = $a[0]; $minK = $a[0]; foreach($a as $k => $v) { if($v > $a[$maxK]) $maxK = $v; if($v < $a[$minK]) $minK = $v; } , . / :$maxKey = $maxK; $maxValue = $a[$maxK]; $minKey = $minK; $minValue = $a[$minK]; +2Ry︁ 04 '11 19:40array_keys, natcasesort., :<?php $testData = array( "2010-05-03" =>100, "2010-05-04" =>400, "2008-05-01" =>800, "2011-01-01" =>800 ); natcasesort($testKeys = array_keys($testData)); echo 'Min: ' . $testKeys[0] . ', max: ' . $testKeys[count($testKeys) - 1] . '<br />'; ?> +2John Parker 04 '11 19:46asort($a); $keys = array_keys($a); $min = $keys[0]; $max = $keys[count($keys) - 1]; ,$min = array_search(min($a), $a); $max = array_search(max($a), $a); +1David Fells 04 '11 19:45More articles:jQuery masked input - date format as m / d / yyyy or m / dd / yyyy or mm / dd / yyyy or mm / d / yyyy - javascriptMy "has_many through" join model has a null reference after saving - ruby-on-railsОшибка, включая fwpmu.h в Visual Studio 2010 - c++Free data mappings / annotations for unique columns and navigation properties with different names in Entity Framework 4.1? - mappingPass any member function of any class as a callback function - c ++Обработка тегов HTML в XSL - javascriptDelphi - Using Interfaces from Another Device - delphiDoes javamail load into memory before sending? - javaErrors when creating a web service client in Java - javaTablesorter jquery plugin crashes in FF 3.6.12 with msg "table.config.parsers [c] is undefined" - jqueryAll Articles
I have an array
$a = array( "2010-05-03" =>100, "2010-05-04" =>400, "2008-05-01" =>800, "2011-01-01" =>800 );
How to find the maximum and minimum on key( date)?
key( date)
For instance:
max => 2011-01-01 min => 2008-05-01
I would be lazy and just let PHP look twice over the array. To find the minimum and the second time, to find the first matching key for this value:
$min_key = array_search(min($a), $a);
Or for maximum:
$max_key = array_search(max($a), $a);
(Not sure if you want the values for the max and min keys or the keys for the max and min values.)
This returns the values for the max / min keys:
$keys = array_keys($a); $min = $a[min($keys)); $max = $a[max($keys));
$maxK = $a[0]; $minK = $a[0]; foreach($a as $k => $v) { if($v > $a[$maxK]) $maxK = $v; if($v < $a[$minK]) $minK = $v; }
, . / :
$maxKey = $maxK; $maxValue = $a[$maxK]; $minKey = $minK; $minValue = $a[$minK];
array_keys, natcasesort.
, :
<?php $testData = array( "2010-05-03" =>100, "2010-05-04" =>400, "2008-05-01" =>800, "2011-01-01" =>800 ); natcasesort($testKeys = array_keys($testData)); echo 'Min: ' . $testKeys[0] . ', max: ' . $testKeys[count($testKeys) - 1] . '<br />'; ?>
asort($a); $keys = array_keys($a); $min = $keys[0]; $max = $keys[count($keys) - 1];
,
$min = array_search(min($a), $a); $max = array_search(max($a), $a);