Hass
Hass

Reputation: 1636

Appending to the same array with foreach

I'm trying to loop a foreach array and add new values to the same array but it's not returning the values;

<?php
    foreach($departments as $department){
        $department['users'] = 10;
    }
?>

but when I return the array, 'users' won't be a part of the list. I tried array_push, with not much luck either.

Upvotes: 2

Views: 71

Answers (1)

nice ass
nice ass

Reputation: 16709

Use a reference:

foreach($departments as &$department){
  $department['users'] = 10;
}

Upvotes: 5

Related Questions