Hi, I know how to use the PHP sizeof function to find out the size of a one-dimensional array, such as sizeof($array), but this only gives me the size of the first dimension of a multidimensional array. Say it’s a two-dimensional array: how can I find out the size of the second dimension? Thanks!
Hi, I know how to use the PHP sizeof function to find out the size of a one-dimensional array, such as sizeof($array), but this only gives me the size of the first dimension of a multidimensional array. Say it’s a two-dimensional array: how can I find out the size of the second dimension? Thanks!
Say you have the following scenario:
$arr[0][0] = “hi1″;
$arr[0][1] = “hi2″;
$arr[1][0] = “hi3″;
$arr[2][0] = “hi4″;
We know that the size of the first dimension is 3. For each of the elements in the first dimension, you can find the size of it’s next dimension as so:
sizeof($arr[0]); //gives 2 from the above
sizeof($arr[1]); //gives 1
sizeof($arr[2]); //gives 1
I haven’t run across a shortcut to finding the combined size of all second dimensions, but you could always put the following routine into your own function:
$x = 0;
foreach ($arr as $a) $x += sizeof($a);
The final value of $x will be 4.
Hope this helps and gives you something to play around with!
The length of the second dimension will just be sizeof($array[$index])
If you add them up in a for loop you’ll get the total for the second dimension