The string I am trying to split is $item[\'category_names\']
which for example contains Hair, Fashion, News
I currently have the following
In your code you are overwritting the $categories variable in each iteration. The correct code would look like:
$categories = '';
$cats = explode(",", $item['category_names']);
foreach($cats as $cat) {
$cat = trim($cat);
$categories .= "" . $cat . " \n";
}
update: as @Nanne suggested, explode only on ','