Arafat Rahman
Arafat Rahman

Reputation: 1015

PHP Multidimensional Array Length

This is a multidimensional PHP array.

$stdnt = array(
    array("Arafat", 12210261, 2.91),
    array("Rafat", 12210262, 2.92),
    array("Marlin", 12210263, 2.93),
    array("Aziz", 12210264, 2.94),
);

I can find out the length of the array. That means

count($stdnt); // output is 4

[
    array("Arafat", 12210261, 2.91),
    array("Rafat", 12210262, 2.92),
    array("Marlin", 12210263, 2.93),
    array("Aziz", 12210264, 2.94)
] ` 

But can't get the internal array length.

How can I ?

Upvotes: 13

Views: 25942

Answers (6)

Babiwise
Babiwise

Reputation: 1

Since there is no function in PHP to yield the "Dimension" and "Length Of Each Dimension" of a multidimensional array (Matrix), we can write a recursive function to obtain all lengths of each dimension:

// Get The Dimension Of An Array
function countdim($array){
    if (is_array($array)){
        return countdim(reset($array)) + 1;
    }else{
        return 0;
    }
}

// Get The Length Of Array
function len($array){
    $dim = countdim($array);
    if ($dim == 0){
        return 0;
    }elseif ($dim == 1) {
        return array(count($array));
    }else{
        $len = array(count($array));
        return array_merge($len, len($array[0]));
    }
}

And Then Call "len($stdnt);" And It Will Give You "array(4, 3)"

Upvotes: 0

Hendi Sama
Hendi Sama

Reputation: 1

  // You may try as per this sample  

  $cars=array(
    array("volvo",43,56),
    array("bmw",54,678)
  );
  $mySubSize=sizeof($cars);
  for ($i=0;$i<$mySubSize;$i++) {
    foreach ($cars[$i] as $value) {
      echo "$value <br>";
    }
  }

Upvotes: 0

Ninki
Ninki

Reputation: 93

According to the hint from Meaning of Three dot (…) in PHP, the following code is what I usually use for this case:

$stdnt = array(
    array("Arafat", 12210261, 2.91),
    array("Rafat", 12210262, 2.92),
    array("Marlin", 12210263, 2.93),
    array("Aziz", 12210264, 2.94),
);
echo count(array_merge(...$stdnt));

The Splat operator "..." will only work,

  • Only if the first level keys are in integer, ie, not "strings"
  • PHP > 5.6

Upvotes: 1

AbraCadaver
AbraCadaver

Reputation: 78994

If you are assuming the subarrays are all the same length then:

$count = count($stdnt[0]);

If you don't know the keys:

$count = count(reset($stdnt));

To get an array with a separate count of each of the subarrays:

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

Upvotes: 13

Affan
Affan

Reputation: 1140

please use sizeof function or count function with recursive

 e.g echo (sizeof($stdnt,1) - sizeof($stdnt)) ; // this will output you 9 as you want .

first sizeof($stdntt,1) ; // will output you 13 . count of entire array and 1 mean recursive .

Upvotes: 2

Suyog
Suyog

Reputation: 2482

The other way to count internal array lengths is to iterate through the array using foreach loop.

<?php 
$stdnt = array(
    array("Arafat", 12210261, 2.91),
    array("Rafat", 12210262, 2.92),
    array("Marlin", 12210263, 2.93),
    array("Aziz", 12210264, 2.94),
);

foreach($stdnt as $s)
{
    echo "<br>".count($s);
}
?>  

Upvotes: 6

Related Questions