douche1936 2016-04-15 01:14
浏览 87
已采纳

计算PHP对象数组中的值

This may be a basic question but I am struggling with a way to count values without doing multiple foreach loops. I have this array of objects (partial list included):

array(51) {
  [0]=>
  object(stdClass)#971 (4) {
    ["hole"]=>
    string(1) "2"
    ["club"]=>
    string(1) "6"
    ["shot_type"]=>
    string(1) "6"
    ["shot_loc"]=>
    string(1) "6"
  }
  [1]=>
  object(stdClass)#970 (4) {
    ["hole"]=>
    string(1) "2"
    ["club"]=>
    string(2) "16"
    ["shot_type"]=>
    string(1) "8"
    ["shot_loc"]=>
    string(1) "1"
  }
  [2]=>
  object(stdClass)#969 (4) {
    ["hole"]=>
    string(1) "2"
    ["club"]=>
    string(2) "19"
    ["shot_type"]=>
    string(1) "3"
    ["shot_loc"]=>
    string(1) "2"
  }
  [3]=>
  object(stdClass)#968 (4) {
    ["hole"]=>
    string(1) "1"
    ["club"]=>
    string(1) "1"
    ["shot_type"]=>
    string(1) "6"
    ["shot_loc"]=>
    string(1) "6"
  }
  [4]=>
  object(stdClass)#967 (4) {
    ["hole"]=>
    string(1) "1"
    ["club"]=>
    string(2) "15"
    ["shot_type"]=>
    string(1) "5"
    ["shot_loc"]=>
    string(1) "3"
  }

The number of objects in the list vary but each object will have the key=>values as shown. What I would like to return is an array with count of each of the values of "hole". Something like this:

`array(18) {
  [1]=> 4
  [2]=> 5
  [3]=> 6`

and so on where the key is each of the values of "hole" and the new array value is the count.

I have attempted forms of count, count(get_object_vars($)), and others but all examples I find are counting the objects. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • drf21989 2016-04-15 01:53
    关注

    Your question is a bit confusing, but this should do the trick for you:

    $holes = [];
    foreach ($array as $object) {
        if (isset($object->hole)) {
            $hole = $object->hole;
            if (!isset($holes[$hole])) {
                $holes[$hole] = 0;
            }
            $holes[$hole]++;
        }
    }
    

    I tested it using this:

    $object1 = (object)['hole'=>'2'];
    $object2 = (object)['hole'=>'3'];
    $object3 = (object)['hole'=>'1'];
    $object4 = (object)['hole'=>'3'];
    
    $array = [$object1,$object2,$object3,$object4];
    
    $holes = [];
    foreach ($array as $object) {
        if (isset($object->hole)) {
            $hole = $object->hole;
            if (!isset($holes[$hole])) {
                $holes[$hole] = 0;
            }
            $holes[$hole]++;
        }
    }
    
    echo "<pre>";
    print_r($holes);
    

    Prints

    Array
    (
        [2] => 1
        [3] => 2
        [1] => 1
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题