13

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 ?

1
  • What is your desired result? Commented Oct 6, 2015 at 4:23

6 Answers 6

13

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);
Sign up to request clarification or add additional context in comments.

1 Comment

The first one is working for both known and unknown keys with some modification. Thanks...:)
6

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);
}
?>  

Comments

2

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 .

Comments

1

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

Comments

0
  // 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>";
    }
  }

1 Comment

It would be nice if you could explain how your code works. There is a bit too much code here for others to copy-paste confidently without understanding it in depth!
0

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)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.