I just wrote this up, is this the most efficient way to add arrays to a preexisting array.
$c=4;
$i=1;
$myarray = array();
while($i <= $c):
array_push($myarray, array('key' => 'value'));
$i++;
endwhile;
echo '<pre><code>';
var_dump($myarray);
echo '</code></pre>';
Update: How would you push the key & value, without creating a new array.
so this array_push($myarray,'key' => 'value');
not this array_push($myarray, array('key' => 'value'));
Your code has a couple of things which can be improved:
Magic Numbers
It’s a bad practice to assign magic numbers like 4 and 1, use constants instead. For this example it’s of course overkill but still important to know and use.
Lack of braces
Always use the curly braces, it makes the code more readable.
Wrong use of while loop
This is not a case for a while loop, if you want to loop a certain number of times, always use a for loop!
Unnessesary use of array_push
You don’t need array push to add elements to an array, you can and should probably use the shorthand function.
Result: