计算多维数组PHP中的值

时间:2016-06-13 11:22:56

标签: php arrays multidimensional-array

我知道这个问题已经回答了几次,但是,没有一个答案适用于我的情景。

当前数组:

    Array
(
    [12] => Array
        (
            [0] => 1AM
            [1] => 2AM
            [2] => 3AM
            [3] => 4AM
        )

    [13] => Array
        (
            [0] => 1AM
            [1] => 2AM
            [2] => 6AM
            [3] => 4AM
        )

    [14] => Array
        (
            [0] => 1AM
            [1] => 2AM
            [2] => 7AM
            [3] => 4AM
        )

)

期望的输出:

3 People Signed Up at 1AM,

3 People signed up at 2AM,

1 Signed up at 3AM

1 Signed up at 6AM

1 Signed up at 7AM

3 Signed up at 4AM

当前代码:

    foreach($array as $k => $v) {
    $result[$k] = array_count_values($v);
    arsort($result[$k]);
}
print_r($result);

换句话说,只计算时间并将它们存储在单独的数组或变量中。

4 个答案:

答案 0 :(得分:11)

您可以像{/ p>一样使用C:\jython2.5.2\bin\jython.bat ${workspace_loc:/jythonTest/uploadScript.py} ${workspace_loc}${selected_resource_path} 127.0.0.1 maxadmin password IOError: [Errno 2] File not found - C:\Users\IBM_ADMIN\workspace\jythonTest\${workspace_loc:\jythonTest\uploadScript.py} (The filename, directory name, or volume label syntax is incorrect) array_count_values

call_user_func_array

答案 1 :(得分:0)

这是我认为最好的解决方案

foreach($array as $item) {
    $result[] = count($item);
}
print_r($result);

答案 2 :(得分:0)

在PHP> = 5.6中,你可以使用参数unpacking而不是调用call_user_func_array():

$result = array_count_values(array_merge(...$array));

了解详情:Argument unpacking via ...

答案 3 :(得分:0)

$counts = array_count_values(array_column($array, 'array_key'));