How to store values from foreach loop into an array in php

Need to store values from foreach loop into an array, need help doing that.

The 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);
}

print_r($items);

How to store values from foreach loop into an array in php

asked Jun 15, 2010 at 13:35

2

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;
}

print_r($items);

answered Jun 15, 2010 at 13:38

Andy EAndy E

330k83 gold badges469 silver badges441 bronze badges

2

Use

$items[] = $username;

answered Jun 15, 2010 at 13:37

SjoerdSjoerd

72.3k16 gold badges127 silver badges171 bronze badges

3

Try

$items = array_values ( $group_membership );

answered Jun 15, 2010 at 13:41

DogbertDogbert

205k40 gold badges384 silver badges392 bronze badges

1

 $username) { 
 $items[$count++] = $username; 
} 
print_r($items); 
?>

Mo.

24.8k36 gold badges150 silver badges219 bronze badges

answered Jun 15, 2010 at 13:38

sushil bharwanisushil bharwani

29.1k30 gold badges92 silver badges128 bronze badges

4

You can try to do my answer,

you wrote this:

 $username) {
    $items = array($username);
}

print_r($items);
?>

And in your case I would do this:

 ' before $username
    $items[] = $username;
} ?>

As you show in your question it seems that you need an array of usernames that are in a particular group :) In this case I prefer a good sql query with a simple while loop ;)


while is faster, but the last example is only a result of an observation. :)

How to store values from foreach loop into an array in php

answered Jun 15, 2010 at 14:20

How to store values from foreach loop into an array in php

MassimoMassimo

5437 silver badges21 bronze badges

This is how you do it:

foreach($orders as $item){
   $transactionIds[]  =   $item->generated_order_id;
}

dump($transactionIds);

This is assuming $orders collection has a field called 'generated_order_id'.

answered Aug 18, 2021 at 8:51

How to store values from foreach loop into an array in php

hackernewbiehackernewbie

1,38416 silver badges13 bronze badges

$items=array(); 
$j=0; 

foreach($group_membership as $i => $username){ 
    $items[$j++]=$username; 
}

Just try the above in your code .

How to store values from foreach loop into an array in php

answered Jun 15, 2010 at 15:56

CuriousCaseCuriousCase

7472 gold badges9 silver badges21 bronze badges

1

Just to save you too much typos:

foreach($group_membership as $username){
        $username->items = array(additional array to add);
    }
    print_r($group_membership);

answered Mar 31, 2020 at 17:24

How to store values from foreach loop into an array in php

this question seem quite old but incase you come pass it, you can use the PHP inbuilt function array_push() to push data in an array using the example below.

 $username) {
        array_push($item, $username);
    }
    print_r($items);
?>

answered Jun 16, 2018 at 17:44

How to store values from foreach loop into an array in php

1

How can we store database values in array using PHP?

Table structure. Create contents_arr table. ... .
Configuration. Create a config. ... .
With serialize() and unserialize() Define two arrays – $names_arr , and $users_arr . ... .
With implode() and explode() Use implode() to separate the $names_arr by separator (” , “) and get a string. ... .
With Loop. Loop on the $users_arr Array. ... .
Conclusion..

How do you store values in an array?

Storing Data in Arrays. Assigning values to an element in an array is similar to assigning values to scalar variables. Simply reference an individual element of an array using the array name and the index inside parentheses, then use the assignment operator (=) followed by a value.

Can we use foreach loop for multidimensional array in PHP?

You can simply use the foreach loop in combination with the for loop to access and retrieve all the keys, elements or values inside a multidimensional array in PHP.

What is the difference between each () and foreach () in PHP?

The for and foreach loop can be used to iterate over the elements. ... PHP..