我如何计算数组中的相同值。我已经阅读了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] => [email protected]
[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] => [email protected]
[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] => [email protected]
[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] => [email protected]
[appointments_enabled] => 0
[role_in_saloon] => Owner
[name] => mubassir
)
)
任何帮助将不胜感激。
您可以从要对其进行聚合/计数的多维数组中取出一列。
<?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
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句