135

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);
2
  • 14
    .= appends text. [] appends onto an array. Commented Jun 15, 2010 at 13:40
  • By far Skilldrick nailed it with the one liner above no need to go any further. Commented Jan 17, 2020 at 19:20

9 Answers 9

308

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Also, don't extract the key ($i) if you're not going to use it.
what would happen if some of the $username are null? We have a similar situation where records are coming form an API, and somehow we are ending up with some null records in the array.
17

Use

$items[] = $username;

3 Comments

Just make sure $items = array(); appears before the loop.
Can you help explain why you have to declare $items = array(); before the loop. I did something like this and didn't declare it and it still works. Is it better to add it or is it not required?
8

Try

$items = array_values ( $group_membership );

1 Comment

Well I assume the foreach loop is doing more than that, otherwise this is the best solution.
8
<?php 
$items = array();
$count = 0;
foreach($group_membership as $i => $username) { 
 $items[$count++] = $username; 
} 
print_r($items); 
?>

4 Comments

No need for $count stuff. Just $array[] = $thing;
I am going to hold off on downvoting this answer although: 1. It is a code-only answer that 2.Teaches developers unnecessary/bad practices ...because this is a great chance to do the Disciplined thing and make Stackoverflow a better resource.
I have issue where my array returns only last element that was pushed into array. Using count as suggested by you resolved my issue.
Great answer for having different values with same name in associative arrays. Thanks.
3

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

Comments

2

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. :)

Comments

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

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

Just try the above in your code .

1 Comment

This code-only answer should not be used by any developers. The counter and incrementing are not necessary at all.
0

Just to save you too much typos:

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

Comments

-1

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

1 Comment

Making iterated function calls is inefficient. The square brace pushing syntax (suggested 8 years earlier) will be more efficient. This answer should not be used to push single elements into an array. (and the declaration of $i is useless)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.