如何找到由键的最大和最小的日期 [英] How to find the maximum and minimum date by key

查看:144
本文介绍了如何找到由键的最大和最小的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组

  $ a =阵列(
           2010-05-03=大于100,
          2010-05-04=> 400,
           2008-05-01=> 800,
          2011-01-01=> 800
     );

我如何找到键(日期)最大和最小

例如:

 最大值=> 2011-01-01
分=> 2008-05-01


解决方案

我想偷懒,只是让PHP看两次数组。一旦找到最小和一个第二时间去寻找该值的第一匹配项:

  $ min_key = array_search(分($ A),$ A);

或为最大:

  $ max_key = array_search(MAX($ A),$ A);

I have an array

$a = array(
           "2010-05-03" =>100,
          "2010-05-04" =>400,   
           "2008-05-01" =>800,
          "2011-01-01" =>800
     ); 

How do I find maximum and minimum by key( date)?

For example:

max => 2011-01-01
min => 2008-05-01

解决方案

I would be lazy and just let PHP look twice over the array. Once to find the minimum and a second time to find the first matching key for that value:

$min_key = array_search(min($a), $a);

Or for the maximum:

$max_key = array_search(max($a), $a);

这篇关于如何找到由键的最大和最小的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆