PHP'foreach'用于逗号分隔值

时间:2013-07-08 12:12:11

标签: php foreach comma

我有一个像这样的变量:
$ variable ='value1,value2,value3,value4,value5';
如何为每个逗号分隔值获取foreach循环?

4 个答案:

答案 0 :(得分:11)

首先尝试将字符串扩展为数组$array = explode(', ',$variable);,然后在结果数组中使用foreach。

答案 1 :(得分:8)

   $variable = 'value1, value2, value3, value4, value5';
   $var=explode(',',$variable);
   foreach($var as row)
    {
    //do your code here
    }

答案 2 :(得分:4)

使用 explode 方法将字符串拆分为数组并迭代数组。 访问爆炸()功能手册http://php.net/manual/en/function.explode.php

<强>代码:

                $variableAry=explode(",",$variable); //you have array now
                foreach($variableAry as $var)
                {
                    echo $var. "<br/>";
                }

答案 3 :(得分:0)

你可以这样做。

    $variable = 'value1, value2, value3, value4, value5';
    $arrs = explode(',', $variable);
    foreach($arrs as $arr){
        //do somthing..
    }

我希望你得到答案

相关问题