[英]How can I create an array in php with 20 rows and 20 columns and their values is 0?
As I know in php, let suppose a variable a is array 正如我在php中所知,假设变量a是数组
$a = array();
the array have keys and its value, and the count of keys is its range, so I thought I could make array in array: 该数组具有键及其值,键的数量是其范围,所以我认为我可以在数组中创建数组:
for($i = 0; $i < 20; $i++)
{
$b = array($i);
$a = array($i=>$b);
}
print_r($a);
but I got --> Array ( [19] => Array ( [0] => 19 ) ) 但是我得到了->数组([19] =>数组([0] => 19))
How can I create an array in php with 20 rows and 20 columns and their values is 0? 如何在php中创建一个包含20行20列且其值为0的数组?
I used array_fill
in php 我用array_fill
在PHP
So I fixed it 所以我修好了
$b = array_fill(0,20,0);
$a = array_fill(0,20, $b);
print_r($a);
Thank you your help 谢谢你的帮助
array_fill(0, 20, array_fill(0, 20, 0));
Try below script 尝试以下脚本
$row_column_array = array();
for($row = 0;$row < 20;$row++){
for($column = 0;$column < 20;$column++){
$row_column_array[$row][$column] = "$row-$column";
}
}
echo "<pre>";
print_r($row_column_array);
output : https://eval.in/747950 输出: https : //eval.in/747950
Try this code, hope it may also help you, as you have already resolved, but keep it in option if necessary: 试试下面的代码,希望它也可以对您有所帮助,因为您已经解决了这个问题,但如有必要,请保留在选项中:
<?php
$a = array();
for($i = 0; $i<20; $i++) {
for($j = 0; $j<20; $j++) {
$a[$i][$j] = 0;
}
}
echo "<pre>";
print_r($a);
?>
Hope, this may be useful to you. 希望这对您有用。
I fixed it 我修好了它
$b = array_fill(0,20,0);
$a = array_fill(0,20, $b);
print_r($a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.