我有一组值,我需要一个特定的输出:
我在表中列出了这些值:
id | value
1 | 1/4
2 | 5/1
3 | 4/1
4 | 6/1
5 | 2/1
6 | 3/1
我正在尝试做什么:
A | B | C | D |
A | 1 | 1/4 | 5/1 | 4/1 |
B | | 1 | 6/1 | 2/1 |
C | | | 1 | 3/1 |
D | | | | 1 |
我只有6个值,我假装输出有10个值
预期输出:
$matrix[0][0] = 1/1;
$matrix[0][1] = 1/4;
$matrix[0][2] = 5/1;
$matrix[0][3] = 4/1;
$matrix[1][1] = 1/1;
$matrix[1][2] = 6/1;
$matrix[1][3] = 2/1;
$matrix[2][2] = 1/1;
$matrix[2][3] = 3/1;
$matrix[3][3] = 1/1;
我正在尝试这样的事情,但没有成功:
while ($sql -> fetch()) {
for ($i = 0; $i < (//something); $i++) {
for ($x = 0; $x < (//something); $x++) {
$matrix[$i][$i+1] = $result;
}
}
}
任何想法?感谢
答案 0 :(得分:0)
请尝试在php中实现此功能。以下是您逻辑的java代码。
Integer[] singleDim = new Integer[] {2,3, 2};
int matrixSiz = 3; //this value has to be set manually.
//i didnt get time to find the logic for this
Integer[][] twoDim = new Integer[matrixSiz][matrixSiz];
int counter = 0; //this iterates through singleDim array
//logic being done for convesion
for (int i = 0; i < matrixSiz; i++) {
for (int j = 0; j < matrixSiz; j++) {
if (i==j) {
twoDim[i][j]=1; //setting the diagonal as 1
}else if (j>i){ // important condition to find the upper part of the matrix
twoDim[i][j]=singleDim[counter++];
}
}
}
//printing it.
for (int i = 0; i < matrixSiz; i++) {
System.out.println(); //new line
for (int j = 0; j < matrixSiz; j++) {
if(twoDim[i][j]==null)
System.out.print("\t");
else
System.out.print("\t"+twoDim[i][j]);
}
}
输出
1 2 3
1 2
1
上述逻辑可以通过更改singleDim
和matrixSiz
来处理任何数据。