php foreach:将每个循环结果放在一个变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3533474/
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
php foreach: put each of the loop result in one variable
提问by laukok
I think this is probably a very simple but I can get my head around! How can I put each of the loop result in one variable only? for instance,
我认为这可能很简单,但我可以理解!如何将每个循环结果仅放在一个变量中?例如,
$employeeAges;
$employeeAges["Lisa"] = "28";
$employeeAges["Hyman"] = "16";
$employeeAges["Ryan"] = "35";
$employeeAges["Rachel"] = "46";
$employeeAges["Grace"] = "34";
foreach( $employeeAges as $key => $value){
$string = $value.',';
}
echo $string;
// result 34,
// but I want to get - 28,16,35,46,34, - as the result
Many thanks, Lau
非常感谢,刘
回答by ircmaxell
回答by Brandon Horsley
回答by Mike
You can also try
你也可以试试
$string = '';
foreach( $employeeAges as $value){
$string .= $value.',';
}
I tried that and it works.
我试过了,它有效。
回答by Tommy
foreach( $employeeAges as $key => $value){
$string .= $value.',';
}
You are resetting your string variable each time through your loop. Doing the above concatenates $value to $string for each loop iteration.
您每次通过循环都会重置字符串变量。执行上述操作将每次循环迭代的 $value 连接到 $string。
回答by Tommy
Um, what about this?
嗯,这个呢?
$string = "";
foreach( $employeeAges as $key => $value){
$string .= $value.',';
}
You are resetting the variable each time, this starts with the empty string and appends something each time. But there are propably betters ways to do such tasks, like implodein this case.
您每次都在重置变量,这从空字符串开始并每次附加一些内容。但是有可能有更好的方法来完成这些任务,比如在这种情况下内爆。
回答by Zaki
Try
尝试
$string = '';
foreach( $employeeAges as $key => $value){
$string .= $value.',';
}
With $string = $value.','; you are overwriting $string every time, so you only get the last value.
$string = $value.','; 你每次都覆盖 $string ,所以你只能得到最后一个值。
回答by Sarfraz
$string .= $value.',';
Use the concatenation, put a dot before the equal sign.
使用串联,在等号前放一个点。
You can use this verbose syntax:
您可以使用以下详细语法:
$string = $string . $value . ',';
回答by Henok
Try this echo must be inside then {}
became ok
试试这个回声一定在里面然后{}
就OK了
$employeeAges;
$employeeAges["Lisa"] = "28";
$employeeAges["Hyman"] = "16";
$employeeAges["Ryan"] = "35";
$employeeAges["Rachel"] = "46";
$employeeAges["Grace"] = "34";
foreach( $employeeAges as $key => $value){
$string = $value.',';
echo $string;
}
// result - 28,16,35,46,34, - as the result
or other way
或其他方式
foreach( $employeeAges as $key => $value){
$string .= $value.',';
}
echo $string;
回答by Odin Thunder
Input:
输入:
$array = [1,2,3,4]
Save all data in one string:
将所有数据保存在一个字符串中:
$string = "";
foreach( $array as $key => $value){
$string .= $value.',';
}
Output:
输出:
$string = '1,2,3,4,'
Remove last comma:
删除最后一个逗号:
$string = rtrim($string, ',');
Output:
输出:
$string = '1,2,3,4'
More information about:
有关的更多信息:
串联;
rrim.