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.
2D阵列的尺寸取决于1D阵列中的元素数量。
Conditions
条件
1.The number of rows of 2D array is fixed as 5.
1.二维数组的行数固定为5。
2.The number of columns may vary.
2.列数可能有所不同。
3.Third row will be empty except for the last element
3.除最后一个元素外,第三行将为空
NOTE
注意
Size of the one dimensional array is varied.
一维阵列的大小是变化的。
We need to get the dimension of 2D array also how can i print it?
我们需要获得2D数组的维度,我该如何打印它?
UPDATE
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
我怎样才能获得$ x的值,它是2D数组的列大小
3 个解决方案
#1
1
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 (e.g sample input 10) the second last column will have a * regardless of the items reaching upto row 3 or not.
假设:如果最后一列中有任何项目(例如样本输入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
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)
测试21,49,50,51。如假设中所述,将显示星号1少于列数。(如果您想通过更改残留检查计数,可以更改它)
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);
$ itemCount = count($ yourArray);
replace the for loop with foreach ($yourArray as $i)
(or change $i with something meaningful all over.)
用foreach替换for循环($ yourArray为$ i)(或用全部有意义的东西改变$ i。)
Output
产量
21
21
1 5 9 13 17
2 6 10 14 18
* * * * 19
3 7 11 15 20
4 8 12 16 21
49
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
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
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
#2
1
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
#3
0
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
#1
1
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 (e.g sample input 10) the second last column will have a * regardless of the items reaching upto row 3 or not.
假设:如果最后一列中有任何项目(例如样本输入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
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)
测试21,49,50,51。如假设中所述,将显示星号1少于列数。(如果您想通过更改残留检查计数,可以更改它)
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);
$ itemCount = count($ yourArray);
replace the for loop with foreach ($yourArray as $i)
(or change $i with something meaningful all over.)
用foreach替换for循环($ yourArray为$ i)(或用全部有意义的东西改变$ i。)
Output
产量
21
21
1 5 9 13 17
2 6 10 14 18
* * * * 19
3 7 11 15 20
4 8 12 16 21
49
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
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
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
#2
1
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
#3
0
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