dtlygweb2017 2014-12-29 01:13
浏览 47
已采纳

根据颜色数量获取最大数量

I have an array that looks like the following (I did a json_encode so it's easier to read on StackOverflow). I'm trying to get the highest color count based on number of colors. So for example single colors like Red has a count of 32 and White has a count of 31. Then I'd like to get the highest value which would be 32. And for dual colors I'd like to get back 4 since [White, Black] has the highest count among the dual colors. And for tri colors I'd like to get back 8 since they are all equal, etc.

[{
        "colors" : "",
        "count" : 55
    }, {
        "colors" : "[\"Red\"]",
        "count" : 32
    }, {
        "colors" : "[\"Green\"]",
        "count" : 32
    }, {
        "colors" : "[\"Blue\"]",
        "count" : 32
    }, {
        "colors" : "[\"White\"]",
        "count" : 31
    }, {
        "colors" : "[\"Black\"]",
        "count" : 31
    }, {
        "colors" : "[\"White\",\"Black\",\"Red\"]",
        "count" : 8
    }, {
        "colors" : "[\"Blue\",\"Red\",\"Green\"]",
        "count" : 8
    }, {
        "colors" : "[\"White\",\"Blue\",\"Red\"]",
        "count" : 8
    }, {
        "colors" : "[\"Blue\",\"Black\",\"Green\"]",
        "count" : 8
    }, {
        "colors" : "[\"White\",\"Black\",\"Green\"]",
        "count" : 8
    }, {
        "colors" : "[\"White\",\"Black\"]",
        "count" : 4
    }, {
        "colors" : "[\"Black\",\"Green\"]",
        "count" : 3
    }, {
        "colors" : "[\"White\",\"Red\"]",
        "count" : 3
    }, {
        "colors" : "[\"Blue\",\"Green\"]",
        "count" : 3
    }, {
        "colors" : "[\"Blue\",\"Red\"]",
        "count" : 3
    }
]

I'd like to retrieve these values in an array. The result would look like this:

$max_color_count = array(55, 32, 4, 8);

The 55 is from the one which has 0 colors, the 32 is from the single colors, the 4 is from dual colors, and the 8 is from tri colors.

  • 写回答

1条回答 默认 最新

  • dongyoufo5672 2014-12-29 01:27
    关注

    This works with array.

    $max_color_count = array(0, 0, 0, 0);
    foreach($data as $k => $v)
    {
      $colors_amount = count($v['colors']);
      $max_color_count[$colors_amount] = max($max_color_count[$colors_amount], $v['count']);
    }
    print_r($max_color_count);
    

    This works with $data = json_decode("JSON_YOU_HAVE_GIVEN");

    $max_color_count = array(0, 0, 0, 0);
    foreach($data as $k => $v) 
    {
        $colors_amount = count(json_decode($v->colors));
        $max_color_count[$colors_amount] = max($max_color_count[$colors_amount], $v->count);
    }
    print_r($max_color_count);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥70 2048小游戏毕设项目