themartin
themartin

Reputation: 85

Counting multidimensional array values

I have an array called $people that looks like the following:

Array
(
    [541377306] => Array
        (
            [0] => 6248267085
        )

    [731082330] => Array
        (
            [0] => 6248267085
            [1] => 229668807087652
        )

    [742088719] => Array
        (
            [0] => 6248267085
        )

    [1133688950] => Array
        (
            [0] => 6248267085
        )

)

You'll see that [731082330] has more values than the rest. How could I have counted that? This seems like it should be easy but none of the examples I can find around the place really match my kind of array. Thanks!

Upvotes: 0

Views: 1968

Answers (4)

Ja͢ck
Ja͢ck

Reputation: 173552

You can apply a regular count() to each element:

$count = array_map('count', $people);

echo $count['731082330']; // 2

This can be sorted and the first key can be picked, assuming you don't care about more than one element having the most values:

arsort($count);
echo key($count);

Upvotes: 1

themartin
themartin

Reputation: 85

Thanks everyone. This is what did it.

//count values
$count = array_map('count', $people);

//find highest
$value =  max($count);

//find value for highest
$mostvalues = array_search($value, $count);

echo $mostvalues;

Upvotes: 0

Vinod VT
Vinod VT

Reputation: 7149

foreach ($array as $key => $value)
{
  echo count($value) . "<br />";
}

Try this

Upvotes: 0

Chintan Parekh
Chintan Parekh

Reputation: 1101

There are many ways you can do it. However, just a small hint. This should work.

foreach ($array1 as $array2){ // array1 contains multiple arrays inside it
            echo count($array2);
        }

Upvotes: 1

Related Questions