Get the dimensions of a 2D array relative to the size of a 1D array

I have one dimensional array like this

$arr=array('1','2','3','4','5','6','7','8','9','10','11','12','13'......'21');

From this, I want to create a two-dimensional array similar to this.

enter image description here

The dimension of the 2D array depends on the number of elements in the 1D array.

Terms

1. The number of rows in a 2D array is fixed as 5.

2. The number of columns may vary.

3. The third line will be empty except for the last item

Note

The size of the one-dimensional array changes.

We also need to get the dimension of the 2D array, how can I print it?

UPDATE

Here is my code

$k=0;
$l=0;
$i=0;
$A=array('1','2','3','4','5','6','7','8','9','10');
//size of 1D array
$size=count($arr);
//2D array
$B=[];
$x=?;//no of columns of 2d array
$y=5;//no of rows of 2d array

for($i=0;$i<$size;$i++){

        $B[k][l]=$A[i];
        $k++;

        if($k==2 && $l!=$x){
                $k++;

        }
        if($k==4){
                $l++;
        }

}

How can I get the value $xis the size of the columns of a 2D array

+4
source share
3 answers

: (: Array ) 1D , .

: - (, 10), * , 3 .

$itemCount = 49;

$residual = $itemCount % 4;

$starCount = ceil($itemCount/4);

if ($residual > 1) {
    $starCount -= 1;
} else if ($residual) {
    $starCount -= 2;
}



$itemsArray = [];
$key = 0;
for ($i = 1 ; $i <= $itemCount ; $i++ ) {
     $key = $key % 5 ;  // fixing the offset and row number
     if ($key == 2 && $starCount) {
         $itemsArray[$key][] = '*';
         $starCount--;
     $key++;
         $itemsArray[$key][] = $i;
     } else {
         $itemsArray[$key][] = $i;
     }  
    $key++; 

}

print_r($itemsArray); //check output

21, 49, 50, 51. 1 , . ( , , )

. , ( -). , .

$itemCount = count($yourArray);

for foreach ($yourArray as $i) ( $i - ).

21

1  5  9  13 17 
2  6  10 14 18 
*  *  *  *  19 
3  7  11 15 20 
4  8  12 16 21

49

1  5  9  13 17 21 25 29 33 37 41 45 
2  6  10 14 18 22 26 30 34 38 42 46 
*  *  *  *  *  *  *  *  *  *  *  47 
3  7  11 15 19 23 27 31 35 39 43 48 
4  8  12 16 20 24 28 32 36 40 44 49

50

1  5  9  13 17 21 25 29 33 37 41 45 49 
2  6  10 14 18 22 26 30 34 38 42 46 50 
*  *  *  *  *  *  *  *  *  *  *  *  
3  7  11 15 19 23 27 31 35 39 43 47 
4  8  12 16 20 24 28 32 36 40 44 48

51

1  5  9  13 17 21 25 29 33 37 41 45 49 
2  6  10 14 18 22 26 30 34 38 42 46 50 
*  *  *  *  *  *  *  *  *  *  *  *  51
3  7  11 15 19 23 27 31 35 39 43 47 
4  8  12 16 20 24 28 32 36 40 44 48

:

<?php

$itemCount = 21;


$array = range(1,$itemCount);// the 1D array 
$residual = $itemCount % 4;

$starCount = ceil($itemCount/4);

if ($residual > 1) {
    $starCount -= 1;
} else if ($residual) {
    $starCount -= 2;
}



$itemsArray = [];
$key = 0;
foreach ($array as $i) {
     $key = $key % 5 ;  // fixing the offset and row number
     if ($key == 2 && $starCount) {
         $itemsArray[$key][] = '*';
         $starCount--;
     $key++;
         $itemsArray[$key][] = $i;
     } else {
         $itemsArray[$key][] = $i;
     }
    $key++;

}

print_r($itemsArray); //check output
+1

. .

:

$arr = array('1','2','3','4','5','6','7','8','9','10', '11');

$arrLength = count($arr);
$columns = ceil($arrLength / 4);
$rows = ($columns == 1) ? (($arrLength > 2) ? $arrLength + 1 : $arrLength) : 5;

echo "Grid dimension: " . $rows . " x " . $columns . "<br />"; 

$output_array = array();
$index = 0;
for($i = 0; $i < $columns; ++$i){
    for($j = 0; $j < $rows; ++$j){
        if($j == 2 && ($i != $columns - 1 || $columns == 1)){
            $output_array[$j][$i] = "*";
            continue;
        }
        $output_array[$j][$i] = isset($arr[$index]) ? $arr[$index] : "";
        ++$index;
    }
}

// display $output_array
for($i = 0; $i < $rows; ++$i){
    for($j = 0; $j < $columns; ++$j){
        echo $output_array[$i][$j] . " ";
    }
    echo "<br />";
}

:

Grid dimension: 5 x 3
1 5 9
2 6 10
* * 11
3 7
4 8 
+1

:

<?php
$arr=array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21');
$len=count($arr);
$key=0;
$rows=5;
$cols=floor(($len - 2)/4) + 1;
for ($j=0;$j<$cols;$j++)
    for ($i=0;$i<$rows;$i++)                    
        if ($i==2 && $j!=$cols-1) {
            $arr2D[$i][$j] = '*';
        } else {        
            $arr2D[$i][$j] = $key>=$len ? "" : $arr[$key++];
        } 
//print array
echo "<table>";
for ($i=0;$i<$rows;$i++){
    echo "<tr>";
    for ($j=0;$j<$cols;$j++)    
        echo "<td width='30px'>" . $arr2D[$i][$j] . "</td>";  
    echo "</tr>";
}
echo "</table>";
?>

:

1   5   9   13  17
2   6   10  14  18
*   *   *   *   19
3   7   11  15  20
4   8   12  16  21
0

Source: https://habr.com/ru/post/1627385/


All Articles