How to explode an array in PHP?

I have a huge array of Strings that I stored from user inputs. I know that there is a way to automatically output all the content in the array using the explode methods. So what is the correct way to do that?

Let’s say I have an array of [apple, orange, grapes, pineapple]. I want to explode them into singular strings like apple, orange, grapes, etc.

Assuming that you just have a string that you want to explode as an array, you can use the explode() function. If you already have an array you can simply loop through them to output all its values.

If you just need to explode them as strings, you can simply use the explode() on it like this

$array =  explode(',', $items);

It will simply split the array items in the separator.

Example

$items = 'apple,banana,grapes,orange';
$array =  explode(',', $items);
foreach ($array as $item) {
    echo "<li>$item</li>";
}

This will output

<li>apple</li>
<li>banana</li>
<li>grapes</li>
<li>orange</li>