Brad
Brad

Reputation: 12262

How to store values from foreach loop into an array?

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

Upvotes: 135

Views: 547316

Answers (9)

hackernewbie
hackernewbie

Reputation: 1712

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

Upvotes: 3

Don Phelix
Don Phelix

Reputation: 24

Just to save you too much typos:

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

Upvotes: 0

Professor
Professor

Reputation: 2154

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

Upvotes: -1

Massimo
Massimo

Reputation: 573

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

Upvotes: 2

CuriousCase
CuriousCase

Reputation: 775

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

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

Just try the above in your code .

Upvotes: 0

sushil bharwani
sushil bharwani

Reputation: 30187

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

Upvotes: 8

Andy E
Andy E

Reputation: 344537

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

Upvotes: 308

Dogbert
Dogbert

Reputation: 222108

Try

$items = array_values ( $group_membership );

Upvotes: 8

Sjoerd
Sjoerd

Reputation: 75588

Use

$items[] = $username;

Upvotes: 17

Related Questions