计算多维数组中的值并将结果保存为新数组

时间:2013-12-08 03:32:52

标签: php multidimensional-array count

这是我的multidim。阵列:

Array
(
    [0] => Array
        (
            [id] => 1
            [language] => English
        )

    [1] => Array
        (
            [id] => 1
            [language] => English
        )

    [2] => Array
        (
            [id] => 1
            [language] => English
        )

    [3] => Array
        (
            [id] => 1
            [language] => English
        )

    [4] => Array
        (
            [id] => 1
            [language] => English
        )

    [5] => Array
        (
            [id] => 1
            [language] => English
        )

    [6] => Array
        (
            [id] => 1
            [language] => English
        )

    [7] => Array
        (
            [id] => 1
            [language] => English
        )

    [8] => Array
        (
            [id] => 1
            [language] => English
        )

    [9] => Array
        (
            [id] => 1
            [language] => English
        )

    [10] => Array
        (
            [id] => 1
            [language] => English
        )

    [11] => Array
        (
            [id] => 1
            [language] => English
        )

    [12] => Array
        (
            [id] => 1
            [language] => English
        )

    [13] => Array
        (
            [id] => 1
            [language] => English
        )

    [14] => Array
        (
            [id] => 1
            [language] => English
        )

    [15] => Array
        (
            [id] => 1
            [language] => English
        )

    [16] => Array
        (
            [id] => 1
            [language] => English
        )

    [17] => Array
        (
            [id] => 1
            [language] => English
        )

    [18] => Array
        (
            [id] => 1
            [language] => English
        )

    [19] => Array
        (
            [id] => 1
            [language] => English
        )

    [20] => Array
        (
            [id] => 1
            [language] => English
        )

    [21] => Array
        (
            [id] => 1
            [language] => English
        )

    [22] => Array
        (
            [id] => 1
            [language] => English
        )

    [23] => Array
        (
            [id] => 1
            [language] => English
        )

    [24] => Array
        (
            [id] => 2
            [language] => Italian
        )

    [25] => Array
        (
            [id] => 1
            [language] => German
        )

    [26] => Array
        (
            [id] => 1
            [language] => German
        )

    [27] => Array
        (
            [id] => 1
            [language] => German
        )

    [28] => Array
        (
            [id] => 1
            [language] => German
        )

    [29] => Array
        (
            [id] => 1
            [language] => German
        )

    [30] => Array
        (
            [id] => 1
            [language] => German
        )

    [31] => Array
        (
            [id] => 1
            [language] => German
        )

    [32] => Array
        (
            [id] => 1
            [language] => German
        )

    [33] => Array
        (
            [id] => 1
            [language] => German
        )

    [34] => Array
        (
            [id] => 1
            [language] => German
        )

    [35] => Array
        (
            [id] => 1
            [language] => German
        )

    [36] => Array
        (
            [id] => 1
            [language] => German
        )

)

我知道如何遍历数组以及如何计算值,但这次我还需要检查[id]是否在一个包含多种语言的数组中,并且它必须完全动态地用于不同的查询。

新数组应如下所示:

Array (  
   [1] => Array(
        "English" => 24,
        "German" => 12
   ),
   [2] => Array(
         "Italian" => 1
   )
)

1 个答案:

答案 0 :(得分:0)

$sorted_array = array("English" => 0, "German" => 0);
foreach($old_array as $a) {
   if(isset($a['language']) && strlen($a['language']) >= 1) {
      switch($a['language']) {

         case 'English' :
           $sorted_array['English']++;
         break;
         case 'German' :
           $sorted_array['German']++;
         break;

      }
   }
}

只是为了让你继续......如果你真的很聪明,你可以使用array_keys为你自动填充您的sorted_array()...(google it)