How to store values from foreach loop into an array?

PhpForeach

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

Use

$items[] = $username;

Solution 3 - Php

<?php 
$items = array();
$count = 0;
foreach($group_membership as $i => $username) { 
 $items[$count++] = $username; 
} 
print_r($items); 
?>

Solution 4 - Php

Try

$items = array_values ( $group_membership );

Solution 5 - Php

You can try to do my answer,

you wrote this:

<?php
foreach($group_membership as $i => $username) {
    $items = array($username);
}

print_r($items);
?>

And in your case I would do this:

<?php
$items = array();
foreach ($group_membership as $username) { // If you need the pointer (but I don't think) you have to add '$i => ' 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 ;)

<?php
$query = "SELECT `username` FROM group_membership AS gm LEFT JOIN users AS u ON gm.`idUser` = u.`idUser`";
$result = mysql_query($query);
while ($record = mysql_fetch_array($result)) { \
    $items[] = $username; 
} 
?>

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

Solution 6 - Php

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

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

Just try the above in your code .

Solution 7 - Php

Just to save you too much typos:

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

Solution 8 - Php

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'.

Solution 9 - 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.

<?php
    $item = array();
    foreach($group_membership as $i => $username) {
        array_push($item, $username);
    }
    print_r($items);
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBradView Question on Stackoverflow
Solution 1 - PhpAndy EView Answer on Stackoverflow
Solution 2 - PhpSjoerdView Answer on Stackoverflow
Solution 3 - Phpsushil bharwaniView Answer on Stackoverflow
Solution 4 - PhpDogbertView Answer on Stackoverflow
Solution 5 - PhpMassimoView Answer on Stackoverflow
Solution 6 - PhpCuriousCaseView Answer on Stackoverflow
Solution 7 - PhpDon PhelixView Answer on Stackoverflow
Solution 8 - PhphackernewbieView Answer on Stackoverflow
Solution 9 - PhpAdeojo Emmanuel IMMView Answer on Stackoverflow