简体   繁体   English

如何在php中获取矩阵格式

[英]How to get matrix format in php

for($i=0;$i<3;$i++){    
  for($j=1;$j<21;$j++){
     echo $values[$i][$j]["score$j"];
  }
}

The above code gives the result as上面的代码给出的结果为

65432123456543212349
  00000000000000000000
  10543212345654321234

Now I want this result to be in matrix(5*4) format.现在我希望这个结果是矩阵(5 * 4)格式。 Eg.例如。

 1=> 6 5 4 3 
     2 1 2 3
     4 5 6 5
     4 3 2 1
     2 3 4 9 

 2=> 0 0 0 0
     0 0 0 0
     0 0 0 0
     0 0 0 0
     0 0 0 0

Thanks in advance.....提前致谢.....

Use modulus to determine if your second loop reaches the 4th element and add a new line plus spacing if so.使用 模数来确定您的第二个循环是否到达第 4 个元素,如果是,则添加一个新行加上间距。

Also add the key and the => part before entering the second foreach.在进入第二个 foreach 之前,还要添加密钥和=>部分。

Something like this :这样的事情:

for($i=0;$i<3;$i++){    
echo "\n" . $i . " => ";    
  for($j=1;$j<21;$j++){
     echo $values[$i][$j]["score$j"]. " ";
     if($j % 4 === 0) {
       echo "\n    ";           
     }
  }
}

If you would provide the content of $values in PHP form, we could give a better solution.如果您以 PHP 形式提供$values的内容,我们可以提供更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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