2016-08-19 22 views
-1

我有一個result array在PHP中。我想根據多個關鍵值對數值進行計數。例如:基於php中的多個鍵值計算值

我想算屬於「fromcity」 =>拉合爾和「tocity」 =>薩希瓦爾

而且也同所有其他fromcity和tocity出發計數所有離境。

像聲明組一樣。

請幫助我。

我有以下結果數組在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" 
      ) 

    ) 
+1

請修改您的問題,以包括您試圖達到預期效果。 – Epodax

回答

0

你必須自己來實現這一點。簡單地遍歷$result_array並計算你需要什麼:

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

echo "My current count is " . $counter; 

你可能想implment計數功能,將剛剛返回櫃檯。

+1

感謝回覆@ivanka Todorova – Faysal

+0

@Ivanka Todorova,如果我想統計所有的值,那麼我需要把這麼多的條件。這樣就不會滿足我的要求。我想統計所有城市的所有班次。我需要的結果數組是拉合爾To Sahiwal(10) – Faysal

+0

對不起,我需要的結果數組是這樣的類型:lahore to sahiwal(8);拉合爾到多(6); multan to islamabad(7) – Faysal

0

如果你想基於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; 
} 

編輯:現在必須正常工作。