PHP explode array then loop through values and output to variable

后端 未结 5 582
眼角桃花
眼角桃花 2020-12-15 11:07

The string I am trying to split is $item[\'category_names\'] which for example contains Hair, Fashion, News

I currently have the following

5条回答
  •  粉色の甜心
    2020-12-15 11:30

    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 ','

提交回复
热议问题