2016-08-19 8 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たいです。あなたはfromcitytocityに基づいてグループ化したい場合は

+1

お返事ありがとう@ivanka Todorova – Faysal

+0

@Ivanka Todorova、もし私がすべての値を数えたいのなら、それ以外の条件が非常に多いです。これで私の要求を満たしているわけではありません。私はすべての都市ですべての出発グループを数えたいと思う。私が必要とする結果配列は、Lahore to Sahiwal(10) – Faysal

+0

です。申し訳ありませんが、必要な結果配列は次のような型です:lahore to sahiwal(8);ラホールからマルタンへ(6);マルチンからイスラマバード(7) – Faysal

0

は、次の操作を行うことができます

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

編集:今では正常に動作している必要があります。

関連する問題