Mehul Chachada
Mehul Chachada

Reputation: 583

How to get number of elements inside a multidimensional array for each element in php

I have an array like this which stores the following information

$array = array
(
  0 => Array
    (
      0=>'some value'
      1=>'some value'
      2=>'some value'
    )
    1 => Array
    (
      0=>'some value'
      1=>'some value'
      2=>'some value'
      3=>'some value'
      4=>'somevalue'
    )
    2 => Array
    (
      0=>'some value'
      1=>'some value'
      2=>'some value'
      3=>'some value'
      4=>'somevalue'
      5=>'somevalue'
    )
)

I need to get count for each element like for 0 there are total 3 elements, and for 1 there are total 5 elements and for 2 six element how can i get
such result

Upvotes: 0

Views: 113

Answers (1)

Don't Panic
Don't Panic

Reputation: 41820

You can map the count function over your array.

$counts = array_map('count', $array);

The result will be an array that corresponds to your original array, with the number of elements in each sub-array as its values.

(So, for the example in your question, $counts would be set to [3, 5, 6].)

Upvotes: 3

Related Questions