如何计算php数组

时间:2017-11-29 05:28:46

标签: php arrays

如何在数组中计算相同的值。我已经读过关于 array_count_values()但是这个函数只能计算整数或字符串值但我的问题是我想在数组中计算相同的日期,我该怎么办?计算数组中的相同日期。

我试过这个array_count_values()但是这给了我这个错误。

A PHP Error was encountered

Severity: Warning

Message: array_count_values(): Can only count STRING and INTEGER values!

Filename: controllers/Dashboard.php

Line Number: 72

Backtrace:

File: /opt/lampp/htdocs/blink_app/application/controllers/Dashboard.php
Line: 72
Function: array_count_values

File: /opt/lampp/htdocs/blink_app/index.php
Line: 315
Function: require_once

这是我的阵列。

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [saloon_staff_id] => 1
            [users_id] => 5
            [ref_id] => 1
            [date] => 2017-11-02
            [time_from] => 12:15:00
            [time_to] => 13:30:00
            [appointment_status] => 2017-11-23
            [saloon_services_id] => 3
            [saloon_profiles_id] => 1
            [phone_number] => 098098098
            [email] => SalmanIq@facebook.com
            [appointments_enabled] => 1
            [role_in_saloon] => Owner
            [name] => Salman Iqbal
        )

    [1] => stdClass Object
        (
            [id] => 2
            [saloon_staff_id] => 2
            [users_id] => 6
            [ref_id] => 2
            [date] => 2017-11-02
            [time_from] => 04:30:00
            [time_to] => 05:00:00
            [appointment_status] => 2017-11-25
            [saloon_services_id] => 2
            [saloon_profiles_id] => 1
            [phone_number] => 98790809809
            [email] => alludin@gmail.com
            [appointments_enabled] => 1
            [role_in_saloon] => No Access
            [name] => Alludin 
        )

    [2] => stdClass Object
        (
            [id] => 1
            [saloon_staff_id] => 1
            [users_id] => 7
            [ref_id] => 3
            [date] => 2017-11-04
            [time_from] => 03:00:00
            [time_to] => 03:30:00
            [appointment_status] => 2017-11-28
            [saloon_services_id] => 2
            [saloon_profiles_id] => 1
            [phone_number] => 098098098
            [email] => SalmanIq@facebook.com
            [appointments_enabled] => 1
            [role_in_saloon] => Owner
            [name] => Salman Iqbal
        )

    [3] => stdClass Object
        (
            [id] => 3
            [saloon_staff_id] => 3
            [users_id] => 7
            [ref_id] => 4
            [date] => 2017-11-28
            [time_from] => 01:00:00
            [time_to] => 02:00:00
            [appointment_status] => 2017-11-28
            [saloon_services_id] => 1
            [saloon_profiles_id] => 1
            [phone_number] => 9080809
            [email] => mubi@yahoo.com
            [appointments_enabled] => 0
            [role_in_saloon] => Owner
            [name] => mubassir
        )

)

任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

您可以从多维数组中取出要进行聚合/计数的列。

<?php

$array = [['name' => 'foo', 'date' => '2017-11-28'], ['name' => 'bar', 'date' => '2017-11-29'], ['name' => 'baz', 'date' => '2017-11-28']];

$dates = array_column($array, 'date'); // here 1st param is the array

这会给你

Array
(
    [0] => 2017-11-28
    [1] => 2017-11-29
    [2] => 2017-11-28
)

现在您可以致电array_count_values,如:

print_r(array_count_values($dates));

Array
(
    [2017-11-28] => 2
    [2017-11-29] => 1
)