tags:

views:

111

answers:

5

what does the assignment in this loop do? I don't get the array notation in it :S

foreach($fieldvalues as $fieldvalue){
    $insertvalues[] = $fieldvalue;
 }
+1  A: 

It inserts a new item at the end of the array.

Other languages tend to have an append or push function for this.

David Dorward
PHP also does - http://php.net/manual/en/function.array-push.php
Finbarr
+5  A: 

Add $fieldvalue to the end of the $insertvalues array.

Finbarr
A: 

It adds the value of $fieldvalue at the end of $insertvalues array. The [] creates an array and values of the specified variable are added to it at the end each time.

Sarfraz
+3  A: 

$insertvalues[] means insert a new item into the array, its a shortcut of array_push(). Its also preferred as it created lesser overhead whilst the PHP is working.

Additional:

For those who are unsure how the loop works.

foreach($fieldvalues as $fieldvalue)

every time the loop... loops, the value $fieldvalue becomes the next value a pointer is looking it in the array $fieldvalues - thus adding this to a new array `$insertvalues by means of the shortcut syntax mentioned above.

Glycerine
The syntax is not actually a shortcut for the `array_push` function; the `array_push` function is distinct as it adds one or more elements to the end of the given array. The `array_push` function is infact a shortcut for a loop over an array of provided elements to add to the end of a given array argument, and the `array_push` function uses the `$array[] = $value` syntax - a language feature - to achieve this.
Finbarr
OK, my semantics were incorrect I agree. I have read [] for a single adding of an item into an array is preferable. Or did I read nonsense?
Glycerine
[] for a single item is preferable, as it avoids the overhead of a function call.
Finbarr
A: 

I think, This is variable for New Array.

AungAung