3 回答
TA贡献1843条经验 获得超7个赞
您可以通过以下方式获取第二维的大小:
$ncols = count($people[$row]);
你的代码可能是这样的:
<?php
$people = array(
array("Rodrick","Java","PHP"),
array("Jane","Python","Javascript"),
array("Tom","Python","R"),
array("Wangari","Ruby","Kotlin"),
);
for($row = 0 ; $row < count($people) ; $row++){
echo "The Programmers ".$row;
echo "<ol>";
$ncols = count($people[$row]);
for($col = 0 ; $col < $ncols ; $col++){
echo "<li>".$people[$row][$col]."</li>";
}
echo "</ol>";
}
?>
TA贡献1852条经验 获得超7个赞
列数在此根据第一行确定。
$numberOfColumns = count(reset($people));
该计算可用于数值数组,也可用于关联数组。
TA贡献1829条经验 获得超7个赞
你真的需要知道嵌套数组的大小吗?
这是foreach使用过的解决方案:
$people = array(
array("Rodrick","Java","PHP"),
array("Jane","Python","Javascript"),
array("Tom","Python","R"),
array("Wangari","Ruby","Kotlin"),
);
foreach($people as $human){
echo "<ol>";
foreach($human as $data){
echo "<li>".$data."</li>";
}
echo "</ol>";
}
- 3 回答
- 0 关注
- 149 浏览
添加回答
举报