将foreach循环的输出存储到变量中 [英] Storing the output of foreach loop into a variable

查看:49
本文介绍了将foreach循环的输出存储到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个foreach循环,它将回显用户从复选框中所做的所有选择.

I have a foreach loop that will echo out all the selection made by the user from the checkboxes.

我试图将值存储到名为 $ getCentralArea 的变量中.但是,当我回显 $ getCentralArea 时,它会显示 4 -仅选中复选框的最后一个值.我应该获得的正确值是 1,2,3,4 .

I am trying to store the value into a variable called $getCentralArea. However, when I echo $getCentralArea it shows 4 - only the last value of the selected checked box. The correct value that I should get is 1,2,3,4.

if(!empty($_POST['centralArea'])) 
{
    foreach($_POST['centralArea'] as $centralArea) 
    {
        $getCentralValue = $centralArea.","; //Output will be in the following format 1,2,3,4
    }
}else{ $getCentralArea="";}

推荐答案

您可以连接起来,但是留下一个逗号.另外,无需循环,只需 implode()数组:

You could concatenate but that leaves a trailing comma. Also, no need to loop, just implode() the array:

$getCentralValue = implode(',', $_POST['centralArea']);

这篇关于将foreach循环的输出存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆