如何将foreach循环中的值存储到数组中? [英] How to store values from foreach loop into an array?
本文介绍了如何将foreach循环中的值存储到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要将foreach循环中的值存储到数组中,需要帮助。下面的代码不起作用,只存储最后一个值,尝试$ items。= ...,但是这不是伎俩,任何帮助将不胜感激。
<$ ($ group_membership as $ i => $ username){
$ items = array($ username); $ p $ <?php
foreach
}
print_r($ items);
?>
解决方案
声明 $ items
数组,并使用 $ items []
将项添加到数组中:
$ items = array();
foreach($ group_membership as $ username){
$ items [] = $ username;
}
print_r($ items);
Need to store values from foreach loop into an array, need help doing that. 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.
<?php
foreach($group_membership as $i => $username) {
$items = array($username);
}
print_r($items);
?>
解决方案
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);
这篇关于如何将foreach循环中的值存储到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文