0

I have a Variable like this:
$variable = 'value1, value2, value3, value4, value5';
How to get a foreach loop for every comma separated value?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Bas
  • 75
  • 1
  • 1
  • 9

4 Answers4

11

Try exploding the string into an array first like this $array = explode(', ',$variable); and then you can use a foreach on the resulting array.

Simon King
  • 453
  • 3
  • 9
11
   $variable = 'value1, value2, value3, value4, value5';
   $var=explode(',',$variable);
   foreach($var as $row)
    {
       //do your code here
       echo $row.'<br>';
    }
Waruna Manjula
  • 2,537
  • 1
  • 24
  • 28
Rajeev Ranjan
  • 4,163
  • 2
  • 27
  • 40
4

Use explode method to split the string to array and iterate the array. Visit the explode() function manual http://php.net/manual/en/function.explode.php

Code:

                $variableAry=explode(",",$variable); //you have array now
                foreach($variableAry as $var)
                {
                    echo $var. "<br/>";
                }
Hearaman
  • 8,117
  • 13
  • 38
  • 56
0

You can do this.

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

I hpe you got your answare