Php count keys in multidimensional array

I have a simple array like so :

stdClass Object
[
    [Colors] => Array
        [
            [0] => stdClass Object
                [
                    [value] => Blue
                ]

        ]

    [Sizes] => Array
        [
            [0] => stdClass Object
                [
                    [value] => 10
                ]
            [1] => stdClass Object
                [
                    [value] => 30

                ]

        ]

]

then I just want to count array keys [Colors] and [Sizes], which should give me 2 in total, but using count[] like count[$array], throws "Warning: count[]: Parameter must be an array or an object that implements Countable"

asked Jul 10, 2019 at 19:58

5

Assuming your variable is $obj

count[get_object_vars[$obj]];
// return 2, for your object variables are two: Colors & Sizes [both arrays]


count[$obj->Colors];
// return 1, for your object variable [Colors] [which is array] has only one element


count[$obj->Sizes];
// return 2, for your object variable [Sizes] [which is array] has two elements


count[get_object_vars[$obj->Sizes[0]]];
// return 1, for your object variable[Size]'s [0] index has only 1 object element, i.e. 10

answered Jul 10, 2019 at 20:13

LIGHTLIGHT

5,45310 gold badges34 silver badges75 bronze badges

[PHP 4, PHP 5, PHP 7, PHP 8]

array_count_valuesCounts all the values of an array

Description

array_count_values[array $array]: array

Parameters

array

The array of values to count

Return Values

Returns an associative array of values from array as keys and their count as value.

Errors/Exceptions

Throws E_WARNING for every element which is not string or int.

Examples

Example #1 array_count_values[] example

The above example will output:

Array
[
    [1] => 2
    [hello] => 2
    [world] => 1
]

See Also

  • count[] - Counts all elements in an array or in a Countable object
  • array_unique[] - Removes duplicate values from an array
  • array_values[] - Return all the values of an array
  • count_chars[] - Return information about characters used in a string

sergolucky96 at gmail dot com

4 years ago

Simple way to find number of items with specific values in multidimensional array:

szczepan.krolgmail.c0m

12 years ago

Here is a Version with one or more arrays, which have similar values in it:
Use $lower=true/false to ignore/set case Sensitiv.

anvil_sa at NOSPAMNO dot hotmail dot com

2 years ago

Based on sergolucky96 suggestion
Simple way to find number of items with specific *boolean* values in multidimensional array:

Dominic Vonk

8 years ago

The case-insensitive version:

rabies dot dostojevski at gmail dot com

15 years ago

I couldn't find a function for counting the values with case-insensitive matching, so I wrote a quick and dirty solution myself:

This prints:

Array
[
    [J. Karjalainen] => 3
    [60] => 2
    [j. karjalainen] => 1
    [Fastway] => 2
    [FASTWAY] => 1
    [fastway] => 1
    [YUP] => 1
]
Array
[
    [J. Karjalainen] => 4
    [60] => 2
    [Fastway] => 4
    [YUP] => 1
]

I don't know how efficient it is, but it seems to work. Needed this function in one of my scripts and thought I would share it.

pmarcIatIgeneticsImedIharvardIedu

19 years ago

array_count_values function does not work on multidimentional arrays.
If $score[][] is a bidimentional array, the command
"array_count_values [$score]" return the error message "Warning: Can only count STRING and INTEGER values!".

How do you calculate the total number of elements in a multidimensional array in PHP?

The count[] function returns the number of elements in an array.

How do I count items in an array PHP?

How to Count all Elements or Values in an Array in PHP. We can use the PHP count[] or sizeof[] function to get the particular number of elements or values in an array. The count[] and sizeof[] function returns 0 for a variable that we can initialize with an empty array.

What is the use of Array_count_values [] in PHP explain with example?

The array_count_values[] function is used to count all the values inside an array. In other words, we can say that array_count_values[] function is used to calculate the frequency of all of the elements of an array. Parameters: This function accepts a single parameter $array.

How do you count elements in an array?

Array#count[] : count[] is a Array class method which returns the number of elements in the array. It can also find the total number of a particular element in the array. Syntax: Array. count[] Parameter: obj - specific element to found Return: removes all the nil values from the array.

Chủ Đề