php 使用 foreach 循环填充数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8332750/
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-26 04:27:40  来源:igfitidea点击:

Fill an array using a foreach loop

phparraysforeach

提问by Michiel

I'm trying to fill an array with my foreach loop, but I don't get it to work. What am I doing wrong?

我试图用我的 foreach 循环填充一个数组,但我没有让它工作。我究竟做错了什么?

$a = array();

$activities = Project::getProjectnames($_DB, $projectnaam);
if(!empty($activities)) {
    foreach($activities as $k => $v) {
          $a .= array_fill($v['name']);
    }
}

All I get back is the string Array...

我得到的只是字符串Array......

回答by Kai Qing

you're concatenating a string there. You need to push the item into the array.

你在那里连接一个字符串。您需要将项目推入数组。

   foreach($activities as $k => $v) {
          $a[] = $v['name'];
    }