Brad Brad - 1 year ago 81
PHP Question

need to store values from foreach loop into array

Need to store values from foreach loop into an array, need help doing that. Code below does not work, only stores the last value, tried $items .= ..., but that is not doing the trick either, any help will be appreciated.

foreach($group_membership as $i => $username) {
$items = array($username);


Answer Source

Declare the $items array outside the loop and use $items[] to add items to the array:

$items = array();
foreach($group_membership as $username) {
 $items[] = $username;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download