这个问题在这里已有答案:
我有一个像这样的变量: $ variable ='value1,value2,value3,value4,value5'; 如何为每个逗号分隔值获取foreach循环?
尝试首先将字符串扩展为数组,如$array = explode(', ',$variable);
,然后您可以在结果数组上使用foreach。
$variable = 'value1, value2, value3, value4, value5';
$var=explode(',',$variable);
foreach($var as row)
{
//do your code here
}
使用explode方法将字符串拆分为数组并迭代数组。访问explode()函数手册http://php.net/manual/en/function.explode.php
码:
$variableAry=explode(",",$variable); //you have array now
foreach($variableAry as $var)
{
echo $var. "<br/>";
}
你可以这样做。
$variable = 'value1, value2, value3, value4, value5';
$arrs = explode(',', $variable);
foreach($arrs as $arr){
//do somthing..
}
我希望你得到你的答案