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

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 Python函数嵌套调用与递归
      • ¥20 不会做,找个好心人帮做,有偿,只要答案,不要说废话
      • ¥15 html页面铺满及插入音乐
      • ¥15 T5模型代码问题求助
      • ¥15 JSON文件存入数据库mysql/MongoDB
      • ¥15 C++实现更大的整数类
      • ¥15 Python怎么实现加减乘除乘方和阶乘呀
      • ¥15 centos8-arm64平台编译安装ray分布式计算框架。
      • ¥20 求帮做一个安卓app,用Android Studio制作。需要整个项目文件。
      • ¥15 使用深度学习对于楼梯尺寸进行检测