0

How I can count same values in the Array. I have read about the array_count_values() but this function can only count Integer or String values but My problem is that I want to count same dates in the Array, How can I count the same dates in the Array.

I have tried this array_count_values() but this gives me this error.

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

And Here Is my Array.

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
        )

)

Any Help Will be Appreciated.

2
  • 1
    Show more. Your question is very incomplete. Every time you post show exactly what you've tried, offer sample input, and your expected output... EVERY TIME. Your question is justifiably closable for a variety of reasons. Please edit your question before the downvotes start rolling in. Commented Nov 29, 2017 at 5:30
  • I was about to downvote but sometimes it make sense to help people on boarding rather making them scared. Commented Nov 29, 2017 at 7:02

1 Answer 1

2

You can take a column out of multi-dimensional array on which you want to do aggregation / count.

<?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

This will give you

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

Now you can call array_count_values like:

print_r(array_count_values($dates));

Array
(
    [2017-11-28] => 2
    [2017-11-29] => 1
)
Sign up to request clarification or add additional context in comments.

9 Comments

How can i convert it multi-dimensional array?
it also gives me this error. Message: array_column() expects parameter 1 to be array, string given
Can you paste your code in the question so that i can see whats happening
@SalmanIqbal How can i convert it multi-dimensional array? Please explain more
Sorry my bad ! array_column first param is array second is the name of column. PHP :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.