I want to know how do I count total number of matrix elements? the count()
function tells me the number of elements of an array, but if i have m[n][n], how do i know how many elements are there? thx
This is the result of var_dump
:
如何使用PHP计算矩阵(2d数组)元素? [关闭]
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- dongxizhe9755 2014-01-09 13:11关注
EDIT:
For any dimensional Array count of elements:
Demo : https://eval.in/87683
$food = array('fruits' => array('orange', 'banana', 'apple'),"dsd"=>array('orange', 'banana', 'apple','dsfdsf'),'sa'=>array('1','1','1','1','1','1','1','1','1','1','1')); $arr = (array_map('count', $food)); //print_r($arr); $c = 0; foreach($arr as $k => $v){ $c += $v; } echo $c;
Max array count: Live demo :https://eval.in/87611
echo max(array_map('count', $your2dArray));
It will return count of 2d array
If you want to get count of the all the elements then :
$array[0][0] = "one"; $array[0][1] = "two"; $array[1][0] = "three"; $array[1][1] = "four"; $c=0; foreach($array as $a){ $c =$c+count($a); } echo $c;
Live demo : https://eval.in/87617
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 像这种代码要怎么跑起来?
- ¥15 怎么改成循环输入删除(语言-c语言)
- ¥15 安卓C读取/dev/fastpipe屏幕像素数据
- ¥15 pyqt5tools安装失败
- ¥15 mmdetection
- ¥15 nginx代理报502的错误
- ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
- ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
- ¥100 描述一下元器件的基本功能,pcba板的基本原理
- ¥15 STM32无法向设备写入固件