简体   繁体   中英

Get dimensions of 2D array with respect to size of 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 look like this.

在此输入图像描述

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

Conditions

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

2.The number of columns may vary.

3.Third row will be empty except for the last element

NOTE

Size of the one dimensional array is varied.

We need to get the dimension of 2D array also 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 of $x it is columns size of 2D array

Try this: (Edit: Array solution also added below) Instead of the 1D array I have used a loop which emulates an array.

Assumptions : if there is any item in the last column (eg sample input 10) the second last column will have a * regardless of the items reaching upto row 3 or not.

$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

Tested for 21, 49, 50, 51. Will show Stars 1 less than number of columns as mentioned in assumption.( it can be changed if you want by changing the residual check count)

Note: I am leaving the printing part as that is upto you(you want to print it on the command line or on a web page). Moreover its just a matter of looping over the result. And for the array version of this code you can put

$itemCount = count($yourArray);

replace the for loop with foreach ($yourArray as $i) (or change $i with something meaningful all over.)

Output

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

Array based solution for reference:

<?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

You can use the following method to generate the desired grid structure. This method is applicable to any arbitrary sized array.

Updated code:

$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 />";
}

Output:

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

This works for any array of any length:

<?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>";
?>

output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM