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

后端 未结 6 1885
野性不改
野性不改 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:40

    If you need to know an actual number, then you can use the sizeof() or count() functions to determine the size of each array element.

    $rows = count($two_darray) // This will get you the number of rows
    
    foreach ($two_darray as $row => $column)
    {
        $cols = count($row);
    }
    
    0 讨论(0)
  • 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++ ) {
            ...
         }
    }
    
    0 讨论(0)
  • 2021-01-07 04:45

    the quick way for normal,non-mixed 2-dim Array,

    $Rows=count($array); $colomns=(count($array,1)-count($array))/count($array);

    0 讨论(0)
  • 2021-01-07 04:46

    This is what i do: My superheroes' array:

    $superArray[0][0] = "DeadPool";
    $superArray[1][0] = "Spiderman";
    $superArray[1][1] = "Ironman";
    $superArray[1][2] = "Wolverine";
    $superArray[1][3] = "Batman";
    

    Get size :

    echo count( $superArray ); // Print out Number of rows = 2
    echo count( $superArray[0] ); // Print Number of columns in $superArray[0] = 1
    echo count( $superArray[1] ); // Print Number of columns in $superArray[1] = 4
    

    php

    0 讨论(0)
  • 2021-01-07 04:50

    For a php Multidimensional Array, Use

    $rowSize = count( $arrayName );
    $columnSize = max( array_map('count', $arrayName) );
    
    0 讨论(0)
  • 2021-01-07 04:52

    for a php indexed two-dimensional array:

    $arName = array(
      array(10,11,12,13),
      array(20,21,22,23),
      array(30,31,32,33)
    );
    
    $col_size=count($arName[$index=0]);
    
    for($row=0; $row<count($arName); $row++)
    {
      for($col=0; $col<$col_size; $col++)
      {
        echo $arName[$row][$col]. " ";
      }
      echo "<br>";
    }
    

    Output:

    10 11 12 13
    20 21 22 23
    30 31 32 33
    
    0 讨论(0)
提交回复
热议问题