Reputation: 289
I have a 'foreach' PHP loop. Each time the loop is run, I am combing the value in the current loop iteration ($CurrentTillID) to a variable.
Here is my code:
$separator = ',';
$tillDetails = $tillDetails . $CurrentTillID;
I am wanting to add a separator between each $CurrentTillID value.
This is the code that I have tried:
$tillDetails = $tillDetails . $CurrentTillID . $separator;
This above code does not work. Can I please have some help to get this working?
Thanks in advance.
Upvotes: 0
Views: 962
Reputation: 46620
Concatenate with .=
<?php
$tillDetails = null;
foreach($data as $k=>$v){
$tillDetails .= $v.',';
}
?>
Tho is you only have a single dimension array you may as well forget the foreach and just implode it into a string with your separator:
$orig_data = array('till1','till2','till3','yada');
$tillDetails = implode(',',$orig_data);
Upvotes: 0
Reputation: 2290
You probably meant to do this:
$tillDetails .= $separator . $CurrentTillID;
Upvotes: 1
Reputation: 324760
Try putting the separator in between the items you are separating.
Upvotes: 2