I have a two-dimensional array $persona[][]
and I want to know how many rows it has.
I used the function count()
, but it tells me all the elements of the array, both rows and columns.
I have a two-dimensional array $persona[][]
and I want to know how many rows it has.
I used the function count()
, but it tells me all the elements of the array, both rows and columns.
<?php
$persona[0][0] = "Daniela";
$persona[0][1] = "Camila";
$persona[0][2] = "Yuli";
$persona[1][0] = "Mariana";
$persona[2] = "Elefante";
$persona[3] = "Cebra";
$persona[4] = "Aguila";
print contarFilas($persona);
function contarFilas($arreglo){
$cantidad = 0;
foreach($arreglo as $elemento){
$cantidad ++;
}
return $cantidad;
}
?>
5
Taking into account the principles of positions in the arrangement, we understand that an arrangement is always defined as:
$arreglo[FILA][COLUMNA];
Using the operator foreach
, we can access each of the "rows" of the array. Well, it could be used to access other values, the only thing we will do is count how many rows we get from that arrangement. Using an increment operator.