Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Multidimensional Array Length

Tags:

arrays

php

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 ?

like image 215
Arafat Rahman Avatar asked Oct 06 '15 04:10

Arafat Rahman


People also ask

How Get length of multi dimensional array in PHP?

Example 3: Find Length of Multidimensional Array In the following program, we are initializing a two dimensional PHP array. Then we are using count() function to find the number of elements in the array. Pass array as first argument and COUNT_RECURSIVE as second argument.

How do you find the length of a multidimensional array?

To get the length of a 2D Array in Python: Pass the entire array to the len() function to get the number of rows. Pass the first array element to the len() function to get the number of columns. Multiply the number of rows by the number of columns to get the total.

How do you determine the length of an array?

To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this: int a[17]; size_t n = sizeof(a) / sizeof(int);

Is multidimensional array PHP?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.


1 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);
like image 131
AbraCadaver Avatar answered Sep 20 '22 17:09

AbraCadaver