2021年3月26日星期五

Output comma separated values in foreach loop

I have this array:

  $bonusarraymain = array(         array( "bonus1"=> "Testitem" ,            ),       array( "bonus2"=> "" ,            ),       array( "bonus3"=> "444" ,            ),       array( "bonus4"=> "" ,            )      );  

I want to echo out the values which aren´t empty. The values should also be separated with a comma between each other. There shouldn´t be a comma after the last value.

This is how I output the values, but how can I separate them with commas?

          foreach ($bonusarraymain as $bonus) {                  echo $bonus['bonus1'];                  echo $bonus['bonus2']['0'];                  echo $bonus['bonus2']['1'];                  echo $bonus['bonus3'];                  echo $bonus['bonus4'];                       }  
https://stackoverflow.com/questions/66827220/output-comma-separated-values-in-foreach-loop March 27, 2021 at 09:31AM

没有评论:

发表评论