Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

arrays - Count values based on multiple key values in php

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

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

And also same all other fromcity and tocity departure counts.

As like group by statement.

Kindly help me out in it.

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

    )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to implement this by yourself. Simply iterate over the $result_array and count what you need:

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

echo "My current count is " . $counter;

You probably want to implment a count function that will just return the counter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[1] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

56.9k users

...