douzhong4222 2019-05-10 06:25
浏览 225
已采纳

php从多维数组中获取最多和最少的值

I want to get most and least occuring values from php sequential/indexes, assoc and multidimensional arrays

Consider the following dataSet

        $dataSet = [
            'users' => 
            [
                'id' => 1,
                'name' => "Alex",
                'username' => 'alex',
            ],
            [
                'id' => 2,
                'name' => "Alex",
                'username' => 'alex'
            ],
            [
                'id' => 2,
                'name' => "Peter Khot",
                'username' => 'peter',
            ]
        ];

Here above are samples dataSet

Here is what that i tried

    function most_occurring(array $array, $key)
    {
        $dataSet = [];
        $i = 0;
        $keys = [];
        foreach ($array as $k) {
            if (in_array($k[$key], $keys)) {
                $keys[$i] = $k[$key];
                $dataSet[$i] = $k;
            } 

            $i++;
        }

        return $dataSet;
    }

I tried above code for most occurring values but not working at all, help me in most and least occuring values from arrays. Thanks

  • 写回答

2条回答 默认 最新

  • drll42469 2019-05-10 07:15
    关注

    Thanks to KIKO Software, you can still use array_count_values() for this cases. You will also need to use array_columns() and array_search() in this situation. You can refer to this link to see how array_search() helps in finding the maximum/minimum value and its corresponding key inside an array. To find the most or least occurrence, simply echo the last 6 variables that I have declared in the last 6 lines.

    EDIT* reference: unset array_push

    *And also, your first data in the assoc array is indexed 'user', but the second data in the assoc array is not indexed and hence it is indexed as 0.

    $dataSet = [
                'users' => 
                [
                    'id' => 1,
                    'name' => "Alex",
                    'username' => 'alex',
                ],
                [
                    'id' => 2,
                    'name' => "Alex",
                    'username' => 'alex'
                ],
                [
                    'id' => 2,
                    'name' => "Peter Khot",
                    'username' => 'peter',
                ]
            ];
    $id = array_column($dataSet,'id');
    $name = array_column($dataSet, 'name');
    $username = array_column($dataSet, 'username');
    $occur_id = array_count_values($id);
    $occur_name = array_count_values($name);
    $occur_username = array_count_values($username);
    $most_occur_id  = array_search(max($occur_id),$occur_id);
    $most_occur_name  = array_search(max($occur_name),$occur_name);
    $most_occur_username  = array_search(max($occur_username),$occur_username);
    $least_occur_id  = array_search(min($occur_id),$occur_id);
    $least_occur_name  = array_search(min($occur_name),$occur_name);
    $least_occur_username  = array_search(min($occur_username),$occur_username);
    

    EDIT*: (In case of multiple highest occurence OR all same occurence, just add below code right below the above code.)

    $flag = true;
    $most_occur_name_list = [];
    $count = 0;
    while($flag)
    {
      $most_occur_name_ct =  max($occur_name);
      if($most_occur_name_ct == $count || $count == 0)
        {
          array_push($most_occur_name_list,array_search($most_occur_name_ct,$occur_name));
          unset($occur_name[array_search($most_occur_name_ct,$occur_name)]);
          $count = $most_occur_name_ct;
          if(count($occur_name) == 0)
          {
              $flag = false;
              break;
          }
        }
      else
        {
          $most_occur_name_ct = $count;
          $flag = false;
          break;
        }
    }
    
    //must reinitialize the array
    $occur_name = array_count_values($name);
    $flag = true;
    $least_occur_name_list = [];
    $count = 0;
    while($flag)
    {
      $least_occur_name_ct =  min($occur_name);
      if($least_occur_name_ct == $count || $count == 0)
        {
          array_push($least_occur_name_list,array_search($least_occur_name_ct,$occur_name));
          unset($occur_name[array_search($least_occur_name_ct,$occur_name)]);
          $count = $least_occur_name_ct;
          if(count($occur_name) == 0)
          {
              $flag = false;
              break;
          }
        }
      else
        {
          $least_occur_name_ct = $count;
          $flag = false;
          break;
        }
    }
    if($most_occur_name_ct == $least_occur_name_ct)
    {
        $most_occur_name_list = [];
        $least_occur_name_list = [];
    }
    echo "<pre>";
    print_r($most_occur_name_list);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败
  • ¥15 基于面向对象的图书馆借阅管理系统