简体   繁体   English

如何在php中创建一个包含20行20列且其值为0的数组?

[英]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.

相关问题 php-如何使用“ +1”,“-20”之类的值对数组进行排序 - php - how to sort an array with values like “+1”, “-20” 如何在20x20大小的2d数组的php中将值存储在空的2d数组中 - how to store values in an empty 2d array in php with 20x20 size 2d array 当表格输出大于20行时,如何使其自动创建另一个页面? - How can i make it automatically create another page when table output is larger than 20 rows? 使用SlimPHP,如何在没有%20的情况下创建干净的URL? - Using SlimPHP, how can I create clean URLs without %20? 如何将字符串截断为 PHP 中的前 20 个单词? - How can I truncate a string to the first 20 words in PHP? 如何使用php从URL替换%20? - How can I replace %20 with - from url using php? 需要访问包含20的php数组中的3个值,是3d数组,我需要访问60次相同的值 - need to access 3 values in a php array that contains 20, is a 3d array and I need to access the same values 60 times PHP-如何根据一个数组有多少个子数组在WordPress中创建动态引导行和列? - PHP - How can I Create Dynamic Bootstrap Rows & Columns in WordPress based on how many sub arrays an array has? 如何在php上创建唯一的20个单字节令牌/密钥/ ID? - How create an unique 20 single byte token/key/ID on php? 如何从此数组中分别找到年龄在16-20岁和20岁以上的用户的活动得分总和 - how find sum of activity score of users i age 16-20 and 20+ separately from this array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM