2011-08-02 33 views
0

我有一個巨大的一組像下面的數組。我想統計一輛汽車在同一個生產商ID下顯示的次數。我該怎麼做解析通過一個數組,並計數

[0] => Array 
     (
      [Make] => Array 
       (
        [ManufacturersName] => Nissan 
        [type] => 4Dr 
        [manufacturersID] => 1 
       ) 

      [Car] => Array 
       (
        [Model] => Mexima 

       ) 


     ) 
[1] => Array 
     (
      [Make] => Array 
       (
        [ManufacturersName] => Nissan 
        [type] => 4Dr 
        [manufacturersID] => 1 
       ) 

      [Car] => Array 
       (
        [Model] => Mexima 

       ) 


     ) 

[2] => Array 
     (
      [Make] => Array 
       (
        [ManufacturersName] => Toyota 
        [type] => 4Dr 
        [manufacturersID] => 2 
       ) 

      [Car] => Array 
       (
        [Model] => Corolla 

       ) 


     ) 

在上面的示例,Maxima在Manufacturerid出現了兩次1

感謝

回答

1

你可以使用數組鍵的屬性,你的優勢在這裏

$count_bucket = array(); 

foreach ($arr as $a) { 
    $manufacturer = $a['Make']['Manufacturer_id']; 
    $car = $a['Car']['Model']; 
    $count_bucket[$manufacturer][$car]++; 
} 

var_dump($ count_bucket);

+0

您可能需要添加一些檢查以查看製造商陣列和車鑰匙是否已存在,並在需要時添加它們。否則PHP會抱怨(輕微,但抱怨) – Flambino

+0

不能正常工作 – Autolycus