9

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'));

kr1zmo
  • 837
  • 3
  • 13
  • 30

5 Answers5

21

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:

define('START', 1);
define('END', 4);

$myArray = array();


for ($i = START; $i < END; $i++)
{
    $myArray[] = array('item' => '1 items');
}
Community
  • 1
  • 1
markus
  • 40,136
  • 23
  • 97
  • 142
  • 1
    Don't `Always use the curly braces`, when adding inline PHP code use the alternative syntax or you're just going to frustrate someone who'll try to figure out which curly bracket closes which block. (because there is always someone on the team who's editor places random tabs through the code) – mhitza Mar 19 '11 at 00:44
  • @mhitza: Indeed. Inline PHP code shouldn't happen that often, though. – markus Mar 19 '11 at 00:46
  • @markus , how can I do the same with the foreach. It would be great if you help. thanks – always-a-learner May 22 '17 at 11:03
7

I'd personally do the following looking at your code:

$myarray = array();
for($i=0;$i<4;$i++){
  $myarray[] = array('item' => '1 items');
}      

According to this, array_push is a little less efficient than $myarray[]

Wiguna R
  • 157
  • 5
  • 19
Prisoner
  • 27,391
  • 11
  • 73
  • 102
3

If you really need to only put a certain value n times into an array starting from a certain index, you could just use array_fill:

$myarray = array_fill($i, $c, array('item' => '1 items'));
NikiC
  • 100,734
  • 37
  • 191
  • 225
2

Your example looks fine to me, although you would most probably replace your array_push function call with:

$myarray[] = array('item' => '1 items');

Which "is" a shorthand syntax for array_push.

Update: For an associative array you just do:

$myarray["item"] = "1 items";

Although with your example you'll just overwrite the value on each iteration.

mhitza
  • 5,709
  • 2
  • 29
  • 52
  • sounds right, if I only wanted to add items instead of arrays, I could just execute $myarray[] = 'item' , '1 items'; / correct? – kr1zmo Mar 19 '11 at 00:22
  • @kr1zmo: You'll likely get an error with that code. Perhaps you mean `$myarray['item'] = '1 items'` ? – drudge Mar 19 '11 at 00:25
  • No, each assignment on it's own. – mhitza Mar 19 '11 at 00:26
  • how do I push this into an array 'item' => '1 items' without creating a whole new array to place it in so not this array('item' => '1 items') – kr1zmo Mar 19 '11 at 00:27
  • I don't understand the question. – mhitza Mar 19 '11 at 00:29
  • ok, if you execute this array_push($myarray,'new item') it adds that item to the array, but you can't set the key which is the first item in 'key' => 'value' – kr1zmo Mar 19 '11 at 00:33
-1
for($i=1; $i < 10; $i++) {
    $option[$i] = $i;
}
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45