The string I am trying to split is $item['category_names'] which for example contains Hair, Fashion, News
I currently have the following code:
$cats = explode(", ", $item['category_names']);
foreach($cats as $cat) {
$categories = "<category>" . $cat . "</category>\n";
}
I want the outcome of $categories to be like the following so I can echo it out later somewhere.
<category>Hair</category>\n
<category>Fashion</category>\n
<category>News</category>\n
Not sure if I am going the right way about this?
In your code you are overwritting the $categories variable in each iteration. The correct code would look like:
update: as @Nanne suggested, explode only on ‘,’