php 如何将foreach循环中的值存储到数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3045619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:30:45  来源:igfitidea点击:

How to store values from foreach loop into an array?

phpforeach

提问by Brad

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

需要将 foreach 循环中的值存储到数组中,需要帮助才能做到这一点。

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.

下面的代码不起作用,只存储最后一个值,尝试过$items .= ...,但这也不起作用,任何帮助将不胜感激。

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

print_r($items);

回答by Andy E

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

$items在循环外声明数组并用于$items[]向数组添加项:

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

print_r($items);

回答by Sjoerd

Use

$items[] = $username;

回答by Dogbert

Try

尝试

$items = array_values ( $group_membership );

回答by sushil bharwani

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

回答by Paper-bat

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

正如您在问题中所展示的,您似乎需要一组特定组中的用户名:) 在这种情况下,我更喜欢带有简单 while 循环的良好 sql 查询;)

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

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

while更快,但最后一个例子只是观察的结果。:)

回答by CuriousCase

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

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

Just try the above in your code .

只需在您的代码中尝试上述操作。

回答by Don Phelix

Just to save you too much typos:

只是为了节省您太多的错别字:

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

回答by Adeojo Emmanuel IMM

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 内置函数 array_push() 使用下面的示例将数据推送到数组中。

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