user2023359
user2023359

Reputation: 289

PHP combining variables

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

Answers (3)

Lawrence Cherone
Lawrence Cherone

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

Julio
Julio

Reputation: 2290

You probably meant to do this:

$tillDetails .= $separator . $CurrentTillID;

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324760

Try putting the separator in between the items you are separating.

Upvotes: 2

Related Questions