Mahesh Giri
Mahesh Giri

Reputation: 1840

Counting Values in MuptiDimensional Array in Php

I currently have the following array:

Array(
    [0] => Array
        (
            [user] => Name 
            [pwd] => jk

         )
    [1] => Array
        (
            [user] => Name 
            [pwd] => ew
            [city]=> pune
        )
    )

I am trying to count second index elements

[1] => Array
        (
            [user] => Name 
            [pwd] => ew
            [city]=> pune
        )

It must show count=2/3(if starts with 1)

Upvotes: 0

Views: 48

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

If I'm not wrong then you need this.

$ararr = Array(Array
        (
            'user' => 'Name',
            'pwd' => 'jk'

         ), Array
        (
            'user' => 'Name',
            'pwd' => 'ew',
            'city'=> 'pune'
        )
    );

$count = '';

foreach($ararr as $key => $value){
    $count .= count($value).'/';
}

echo substr($count,0,-1); // 2/3

and using array_map function

$count = array_map(function ($t){ return $result = 'count is '.count($t);},$ararr);
print_r($count); //Array ( [0] => count is 2 [1] => count is 3 )

Upvotes: 1

Related Questions