Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

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/>";
                }

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top