【问题标题】:How to generate zeros array in php [duplicate]如何在php中生成零数组[重复]
【发布时间】:2021-10-23 15:05:21
【问题描述】:

我需要在 php 中生成 zeros 数组,但不知道如何使用 php 生成它。我可以用js来做。

在js数组中的零代码是:

for (let _i = 0; _i < 25; _i++) {
   let n = new Array(_i).fill(0)
   console.log(n);
}

像这样生成:

[]
[ 0 ]
[ 0, 0 ]
[ 0, 0, 0 ]
[ 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

如何使用 php 数组生成这样的内容?

for (let _i = 0; _i < 25; _i++) {
  let n = new Array(_i).fill(0)
  console.log(n);
}

【问题讨论】:

  • array_fill如何使用?
  • for ($i = 0; $i &lt; 25; $i++) echo json_encode(array_fill(0, $i, 0)).PHP_EOL;3v4l.org/AL1o3
  • @lawrence-cherone 工作。谢谢
  • ”; } ?>

标签: php


【解决方案1】:

array_fill()

for ($i = 0; $i < 25; $i++) {
    $n = array_fill(0, $i, 0);
    echo implode(', ', $n) . PHP_EOL; // output imploded for readable reasons
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多