繁体   English   中英

根据php中的多个键值计算值

[英]Count values based on multiple key values in php

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

我要计算属于“ fromcity” =>拉合尔和“ tocity” => Sahiwal的所有离开

同样,所有其他城市和城市出发人数也一样。

就像按声明分组。

请帮助我。

我在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"
            )

    )

您必须自己实现。 只需遍历$result_array并计算所需数量:

$counter = 0;
foreach($result_array as $item) {
  if($item['fromcity'] == 'Lahore' and $item['tocity'] == 'Sahiwal') {
    $counter++;
  }
}

echo "My current count is " . $counter;

您可能想实现一个只返回计数器的计数函数。

如果要基于fromcitytocity ,可以执行以下操作:

$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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM