I'm wondering is there any method (that doesn't use loop or recursion) to create and fill with values an array.
To be precise I want to have an effect of
$t = array();
for($i = 0; $i < $n; $i){
$t[] = "val";
}
But simpler.
I'm wondering is there any method (that doesn't use loop or recursion) to create and fill with values an array.
To be precise I want to have an effect of
$t = array();
for($i = 0; $i < $n; $i){
$t[] = "val";
}
But simpler.
$a = array();
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
$a[] = "value";
you get the idea
I think you can use
$array = array_pad(array(), $n, "val");
to get the desired effect.
See array_pad() on php.net
It depends what you mean. There are functions to fill arrays, but they will all use loops behind the scenes. Assuming you are just looking to avoid loops in your code, you could use array_fill:
// Syntax: array_fill(start index, number of values; the value to fill in);
$t = array_fill(0, $n, 'val');
I.e.
<?php
$t = array_fill(0, 10, 'val');
print_r($t);
?>
Will give:
Array (
[0] => val
[1] => val
[2] => val
[3] => val
[4] => val
[5] => val
[6] => val
[7] => val
[8] => val
[9] => val
)