开发者

php count matching values in a multidimension array

hi everyone i been struggling with this for the last few hours i have this array

Array ( 
        [0] => Ar开发者_如何转开发ray ( [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)     
      ) 

so what i want to do here is to count the [countThis_id] for matchs in certain condition exemple :

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

heres what i want as a final result

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

any idea?thank u


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

And for sorting you can use array_multisort()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜