用于逗号分隔值的PHP'foreach'[重复]

问题描述 投票:0回答:4

这个问题在这里已有答案:

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

php foreach comma
4个回答
11
投票

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


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

4
投票

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

码:

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

0
投票

你可以这样做。

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

我希望你得到你的答案

© www.soinside.com 2019 - 2024. All rights reserved.