Just trying to concatenate using this function

Hi there.

Hi there, I am just trying to use implode function to join the values together.

In the table field I have values stored as:

ProductIDField
|40|38|39|

And these values are separated by |.

Now I’d like to add(concatenate) a new ID eg 54 to it. To store in the table as:

ProductIDField
|40|38|39|54|

But to add 54, I must first separate the existing values using the explode function. I retrieve the existing values into an array variable called:

$ProductInfo[productId];

Now I explode them into variable to separate | from the values.

$currentPrdIds[] = explode("|", $ProductInfo[productId]);

And when we view the values of $currentPrdIds, we get:

print($currentPrdIds);
Array ([0] => Array ( [0] => [1] => 40 [2] => 38 [3] => 39)) 

All is Good. Now to add 54 to it, which is stored in $myPrdID variable. I am Simply typing:

$currentIds[] = implode("|",$currentPrdIds) . "|" . $myPrdID ;

This should give:

|40|38|39|54|

But I’m getting stuck here. It is not joining 54 to the existing values.

Please see if there is any missing step here. I am stuck at the last stage to use implode() function to join array values to a new one.

Please see whats the missing ingredient here. Or is there anything else we should do here.

All Comments and feedbacks are always welcomed! :slight_smile:

Thank you!

I dont see why you need to explode and join again. If what you are getting is a string, you just concatenate more strings.


$ProductInfo['productId'] = "|40|38|39|54|" ;

$new = $ProductInfo['productId'] . "$myPrdID|" ;

echo $new;

ps I quoted your array keys
pps if $myPrdID was an int the above will turn it into a string
ppps you should not generally be storing keys such as 54, 45 etc as a string and exploding them without good reason - preferring to correctly normalize your tables, but that will depend on your situation of course