comma between arrays

2

I'm bringing the following information from a form:

$seg = "Seg";
$ter = "Ter";
$qua = "Qua";
$qui = "Qui";

In many cases, some of these fields will be empty, eg:

$seg = "Seg";
$ter = "Ter";
$qua = "";
$qui = "Qui";

I would like to get this information and put a comma between words, like this:

Seg,Ter,Qui

I tried the following, but it did not work:

$diasPromocoes = $seg.",".$ter.",".$qua.",".$qui;
$dividir = explode(",",$diasPromocoes); 
echo implode(",",$dividir);

Returning:

Seg,Ter,,Qui
    
asked by anonymous 03.02.2016 / 18:12

3 answers

1
$diasPromocoes = array($seg,$ter,$qua,$qui);

// O array_filter com callback vai limpar os campos vazios do array
$diasPromocoes = array_filter($diasPromocoes, function($item){ 
    return !empty($item);
});

echo implode(',', $diasPromocoes);
    
03.02.2016 / 18:17
0

Put variables in an array and make implode .

Example

<?php

$arr = ['Segunda', 'Terça', 'Quarta', 'Sexta'];

echo implode(',', $arr);
    
03.02.2016 / 18:16
0

You can transform the variables into an array and use the array_filter function:

$diasPromocoes = array($seg,$ter,$qua,$qui);
$arrayLimpa = array_filter($diasPromocoes);
echo arrayLimpa;
    
03.02.2016 / 18:23