Search code examples
phploopsforeachexplode

How to loop over array created by explode()?


I have a string with multiple values that are separated with a comma. If I use explode to separate these values as follows, how can I create a foreach loop to do something with each of them? Specifically I would like to create a link / anchor tag for each of them.

My explode statement:

<?php $separated = explode(",", $myString); ?>

Example string:

value1, value2, value3, value4, value5, ...

Solution

  • Something like this

    $separated = explode(',', $myString);
    
    foreach ($separated as $value) {
        echo $value;
    
        // In order to display them in uppercase,
        // just do echo strtoupper($value);
    }