开发者

Creating an array with $x elements in PHP

How would I create an array in PHP that has 开发者_StackOverflow$x empty elements? The $x is unknown and varying value. For example, if I wanted to create an array of 3 elements, I could just do:

$array = array(null,null,null);

However, I don't know what $x is and there could be million elements, I need to do this automatically.


As usual with PHP there's a function for this:

  • array_fill() - Fill an array with values

Example:

$array = array_fill(0, $x, 'value');

This will create an array filled with the $x elements valued 'value' starting at array offset 0.


You can do it like this:

array_fill(0, $x, 'value')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜