0

I'm trying to add a comma between outputs if they exist. If I use implode, comme is display though output string is empty.

$data1 = $_GET['data1']; // red
$data2 = $_GET['data2']; //
$data3 = $_GET['data3']; //
$data4 = $_GET['data4']; // green

$result = implode(', ', array($data1, $data2, $data3, $data4));
echo $result; 
// red, , , green :(
// red, green :)

UPDATE I have multiple keys, which is not the same as the duplicate

www.mysite.com/shoes?size=43 > size is 43
www.mysite.com/shoes?material=leather > material is leather
www.mysite.com/shoes?size=44&color=green > size is 44, color is green
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • How is that different than the duplicate since you pass all the values into an array. You can still filter the empty elements out of your array. – dirkgroten Oct 03 '19 at 17:24

1 Answers1

0

You might want to send colors as an array:

http://my-site.com/?colors[]=red&colors[]=green&colors[]=

Then you can simply do:

$result = implode(', ', array_filter($_GET['colors']));
ka_lin
  • 9,329
  • 6
  • 35
  • 56