【问题标题】:find max value of a key in array在数组中找到一个键的最大值
【发布时间】:2016-04-24 01:37:02
【问题描述】:

我有一个数组

 [DoctorEducation] => Array
            (
                [0] => Array
                    (
                        [id] => 24
                        [user_id] => 91
                        [degree_type_id] => 1
                        [college_hospital] => sms
                        [diploma_name] => 
                        [specialization_id] => 0
                        [start_date] => 02/2009
                        [end_date] => 03/2012
                        [year_passing] => 0000
                        [created] => 2015-10-09 13:14:23
                        [updated] => 2015-10-09 13:16:18
                    )

                [1] => Array
                    (
                        [id] => 26
                        [user_id] => 91
                        [degree_type_id] => 5
                        [college_hospital] => sms
                        [diploma_name] => 
                        [specialization_id] => 48
                        [start_date] => 03/2012
                        [end_date] => 05/2014
                        [year_passing] => 0000
                        [created] => 2015-10-09 13:16:18
                        [updated] => 2015-10-09 13:16:18
                    )

            )

现在我想找出哪个索引,即01 等具有degree_type_id 的最大值。例如,在当前数组索引中,1 的最大值为 degree_type_id,即 5。

我从 DB 得到这个。这是查询

 $fields = array(
        'User.id',
        'User.first_name',
        'User.last_name',
        'User.gender',
        'User.dob',
        'User.image',
        'Specialization.name',
        'User.age'
    );
    $getSpecialist = $this->User->find('all', array('fields' => $fields), array('conditions' => array('User.role_id' => 3, 'User.status' => 1)));

【问题讨论】:

  • 如果您从数据库中获取此信息,为什么不使用您的数据库查询为您获取它
  • @MarkBaker:是的,我从 DB 得到这个。检查我更新的问题以进行查询。
  • $maxId = array_search(array_column($myArray['DoctorEducation'], 'degree_type_id'), max(array_column($myArray['DoctorEducation'], 'degree_type_id')));
  • 你在用框架orm吗?
  • 我正在使用框架cakephp

标签: php arrays


【解决方案1】:

试试这样的:

    $max_index = null;
    $max_value = 0;
    foreach($DoctorEducation as $key => $array){
      if($array['degree_type_id'] > $max_value){
        $max_value = $array['degree_type_id'];
        $max_index = $key;
      }
    }
print_r($DoctorEducation[$max_index]);

这会为您提供 degree_type_id

最高的键的索引和值

【讨论】:

  • 谢谢。有效。我不知道我怎么想念这种方法。我觉得困了.. :D ;)
【解决方案2】:

可能有更快的方法来做到这一点,但这是我的解决方案:

$to_compare = array(); 
foreach($DoctorEducation as $idx => $arr){
    $to_compare[$idx] = $arr['degree_type_id'];
}

$max = $to_compare[array_search(max($to_compare), $to_compare)];

【讨论】:

    【解决方案3】:

    很简单,有几个功能:

    $key = array_search(max(array_column($array['DoctorEducation'], 'degree_type_id')),
                        $array['DoctorEducation']);
    
    • 将相应的列提取到数组中
    • 在该列数组中查找最大值
    • 在该列数组中搜索最大值,返回键

    如果最大值出现不止一次,那么您将得到第一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      相关资源
      最近更新 更多