根据php中的多个键值对值进行计数 [英] Count values based on multiple key values in php

查看:34
本文介绍了根据php中的多个键值对值进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中有一个 result数组.我想基于多个键值来计算值.例如:

I have a result array in php. I want to count values based on multiple key values. For example:

我想计算所有属于"fromcity" =>拉合尔和"tocity" => Sahiwal的出境

I want to count all departure which belongs to "fromcity" => Lahore and "tocity" => Sahiwal

其他城市和城市出发人数也一样.

And also same all other fromcity and tocity departure counts.

就像按声明分组一样.

请帮助我.

我在php中有以下结果数组:

I have a following result array in php:

$result_array = array
    (
        [0] => array
            (
                "id" => "348",
                "departure" => "00:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Buhawalpur"
            )

        [1] => array
            (
                "id" => "531",
                "departure" => "00:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Murree"
            )

        [2] => array
            (
                "id" => "532",
                "departure" => "00:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Rawalpindi"
            )

        [3] => array
            (
                "id" => "581",
                "departure" => "00:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Multan"
            )

        [4] => array
            (
                "id" => "582",
                "departure" => "00:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Sahiwal"
            )

        [5] => array
            (
                "id" => "528",
                "departure" => "00:05:00",
                "fromcity" => "Lahore",
                "tocity" => "Sahiwal"
            )

        [6] => array
            (
                "id" => "1584",
                "departure" => "00:15:00",
                "fromcity]" => "Lahore",
               "tocity]"=> "Multan"
            )

        [7] => array
            (
                "id" => "1586",
                "departure" => "00:15:00",
                "fromcity"=> "Lahore",
                "tocity" => "Sahiwal"
            )

        [8] => array
            (
                "id" => "349",
                "departure" => "01:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Multan"
            )

        [9] => array
            (
                "id" => "529",
                "departure" => "01:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Sahiwal"
            )

        [10] => array
            (
                "id" => "906",
                "departure" => "01:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Rawalpindi"
            )

        [11] => array
            (
                "id" => "685",
                "departure" => "01:45:00",
                "fromcity" => "Lahore",
                "tocity" => "Rawalpindi"
            )

        [12] => array
            (
                "id" => "350",
                "departure" => "02:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Multan"
            )

        [13] => array
            (
                "id" => "530",
                "departure" => "02:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Sahiwal"
            )

        [14] => array
            (
                "id" => "907",
                "departure" => "02:30:00",
                "fromcity" => "Lahore",
                "tocity" => "Rawalpindi"
            )

        [15] => array
            (
                "id" => "736",
                "departure" => "03:15:00",
                "fromcity" => "Lahore",
                "tocity" => "Sahiwal"
            )

        [16] => array
            (
                "id" => "908",
                "departure" => "05:30:00",
                "fromcity" => "Lahore",
                "tocity" => "Rawalpindi"
            )

        [17] => array
            (
                "id" => "1649",
                "departure" => "05:30:00",
                "fromcity"=> "Lahore",
                "tocity" => "Faislabad"
            )

        [18] => array
            (
                "id" => "331",
                "departure" => "06:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Multan"
            )

        [19] => array
            (
                "id" => "438",
                "departure" => "06:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Sialkot"
            )

        [20] => array
            (
                "id" => "447",
                "departure" => "06:00:00",
                "fromcity" => "Lahore",
                "tocity" => "Daska"
            )

    )

推荐答案

如果要基于 fromcity tocity 进行分组,可以执行以下操作:

If you want to group based on fromcity and tocity, you can do the following:

$count = [];
foreach($result_array as $element) {
   $search = $element['fromcity']. '-'. $element['tocity'];
   if(!isset($count[$search])) {
      $count[$search] = 1;
   } else {
      $count[$search] += 1;
   }
}

//Result: 
foreach($count as $name => $cnt) {
 echo $name.' - '. $cnt .PHP_EOL;
}

这篇关于根据php中的多个键值对值进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆