Home » Php » PHP Multidimensional Array Length

PHP Multidimensional Array Length

Posted by: admin July 12, 2020 Leave a comment

Questions:

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 ?

How to&Answers:

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

Answer:

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

Answer:

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 .