PHP explode array then loop through values and output to variable

后端 未结 5 581
眼角桃花
眼角桃花 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:36

    if you use this:

    $cats = explode(", ", $item['category_names']);
    foreach($cats as $cat) {
    $categories = "" . $cat . "\n";
    }
    

    the $categories string is overwritten each time, so "hair" and "fasion" are lost..

    if you however add a dot before the equal sign in the for loop, like so:

    $cats = explode(", ", $item['category_names']);
    foreach($cats as $cat) {
    $categories .= "" . $cat . "\n";
    }
    

    the $catergories string will consist of all three values :)

提交回复
热议问题