user3603645
user3603645

Reputation: 11

Multi-dimensional table length

I have the below array which is going to be larger:

$numbers = array(
array(1,2,3,5,9,8,6),
array(1,5,7,2,3,0),
array(2,5,9,25,78,89)
);

How do i get the length of the arrays which are in the array?

Upvotes: 0

Views: 28

Answers (2)

Parag Tyagi
Parag Tyagi

Reputation: 8960

Try this -

$numbers = array( 
            array(1,2,3,5,9,8,6), 
            array(1,5,7,2,3,0), 
            array(2,5,9,25,78,89)
         );
echo count($numbers);  // Count of outer array ==> 2


$counts = array();
foreach($numbers as $number)
{
    $counts[] = count($number);
}

print_r($counts);  // Array of count of inner
echo array_sum($counts);  // Total Count of inner arrays ==> 19

Upvotes: 0

Masum Nishat
Masum Nishat

Reputation: 368

Take a look here,

<?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

?>

Upvotes: 1

Related Questions