In php, Number of rows and columns in a 2 D array?

后端 未结 6 2023
野性不改
野性不改 2021-01-07 04:38

I have a two dimensional array with unknown number of elements.

$two_darray[row][column]; //there will be an unknown integer values instead of row and column

6条回答
  •  盖世英雄少女心
    2021-01-07 04:42

    foreach ($two_darray as $key => $row) {
       foreach ($row as $key2 => $val) {
          ...
       }
    }
    

    No need to worry about how many elements are in each array, as foreach() will take care of it for you. If you absolutely refuse to use foreach, then just count() each array as it comes up.

    $rows = count($two_d_array);
    for ($row = 0; $row < $rows; $row++) {
         $cols = count($two_darray[$row]);
         for($col = 0; $col < $cols; $col++ ) {
            ...
         }
    }
    

提交回复
热议问题