Problems with explode [closed]

It’s because you’ve created an array with explode and assigned it to $string, then you’re appending strings onto it when you use the .= concatenation operator within your foreach loop.

here’s a simpler method:

$string = '-' . implode( ',-', explode( ' ', $data['exclude_categories'] ) );