woninana
woninana

Reputation: 3481

Count the total number of keys in first array

Array
(
    [18] => Array
        (
            [0] => 137585189
            [1] => 138053588
        )

    [19] => Array
        (
            [0] => 137626141
            [1] => 137672213
            [2] => 137718802
        )
)

Array
(
    [18] => Array
        (
            [0] => 137585189
            [1] => 138053588
        )

    [19] => Array
        (
            [0] => 137626141
            [1] => 137672213
            [2] => 137718802
            [3] => 137732801
        )
)

This is the result from:

foreach($value as $val){
  echo '<pre>';
  print_r($value);
  echo '</pre>';
        }

How can sum the total number of keys per Array?

Array = 5

Array = 6

Upvotes: 2

Views: 436

Answers (3)

Vijaya Pandey
Vijaya Pandey

Reputation: 4282

Try this:

<?php


$array = array(array(array(137585189,138053588),array(137626141,137672213,137718802)), array(array(137585189,138053588), array(137626141,137672213,137718802,137732801)));



foreach($array as $val){
    echo '<pre>';
    print_r($val);
    echo (count($val,COUNT_RECURSIVE)-count($val,0));

        }

?>

Demo Here>>

Upvotes: 1

user568109
user568109

Reputation: 47993

You can do recursive count, by giving option COUNT_RECURSIVE. You can get what you want by subtracting the recursive count with simple count

<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
              'veggie' => array('carrot', 'collard', 'pea'));

// recursive count
echo count($food, COUNT_RECURSIVE); // output 8

// normal count
echo count($food); // output 2

// to count second level entries
echo (count($food,COUNT_RECURSIVE)-count($food,0)); //output 6

?>

Upvotes: 3

Anujan
Anujan

Reputation: 938

You can use COUNT_RECURSIVE like so

count($arr, COUNT_RECURSIVE);

Note that this includes the inner array itself so it'll end up being 7 for the first array. To fix this, you can just subtract count($arr)

Upvotes: 1

Related Questions