php count совпадающие значения в многомерном массиве - PullRequest
0 голосов
/ 07 мая 2011

Привет всем, я боролся с этим в течение последних нескольких часов, у меня есть этот массив

Array ( 
        [0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
        [1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
        [2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
        [3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
        [4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
        [5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
        [6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
        [7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)     
      ) 

, поэтому я хочу подсчитать [countThis_id] для совпадений в определенном состоянии1004 *

      count first = if  [countThis_id] == 2 
      count second = if  [countThis_id] == 1 
      count the rest ignoring the matchs

вот что я хочу в качестве конечного результата

Array ( 
    [0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
    [2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
    [4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
    [5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
    [6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
    [7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)   
    [8] => Array ( [count_1] => 2 [count_2] => 2 [the_Rest] => 4)   
  ) 

есть идеи? Спасибо u

1 Ответ

0 голосов
/ 07 мая 2011
$result = array('count_1' => 0, 'count_2'=>0, 'the_Rest'=>0);
foreach($array as $arr){
   if($arr['countThis_id'] == 2){
     $result['count_1']++;
   }
   else if($arr['countThis_id'] == 1){
     $result['count_1']++;
   }
   else {
     $result['the_rest']++;
   }
}

array_push($array, $result);
var_dump($array);

А для сортировки вы можете использовать array_multisort ()

...