tags:

views:

76

answers:

3

Is there a better syntax for this array insertion in PHP? The array already has items and I'm adding items to it. I hate that I have to repeat the array name multiple times like this

$somearray[] = "new item 1"; 
$somearray[] = "new item 2"; 
$somearray[] = "new item 3"; 
+12  A: 
array_push($somearray, "new item 1", "new item 2", "new item 3");
deceze
+1 Nice, didn't know `array_push()` accepted an arbitrary number of parameters `printf()`-style.
BoltClock
While the OP's method is still WAY readable. it is like operators in the code - horizontal positioning is a pain
Col. Shrapnel
@Col You can just break up the above over several lines if it's a concern. :) It greatly depends on the context though what's the most readable.
deceze
+2  A: 

how about this?

array_merge($somearray, array('new item 1', 'new item 2', 'new item 3'));
leonardys
A: 

I like this:

$array = array('new item 1', 'new item 2', 'new item 3');

foreach($array as $item) {
   $somearray[] = $item;
}
Wolfy