Ryan Litwiller Ryan Litwiller - 16 days ago 8
PHP Question

implode() string, but also append the glue at the end

Trying to use the

implode()
function to add a string at the end of each element.

$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("@txt.att.net,", $array);

print($attUsers);


Prints this:

9898549130@txt.att.net,9898549131@txt.att.net,9898549132


How do I get
implode()
to also append the glue for the last element?

Expected output:

9898549130@txt.att.net,9898549131@txt.att.net,9898549132@txt.att.net
//^^^^^^^^^^^^ See here

Answer

This was an answer from my friend that seemed to provide the simplest solution using a foreach.

$array = array ('1112223333', '4445556666', '7778889999');

// Loop over array and add "@att.com" to the end of the phone numbers
foreach ($array as $index => &$phone_number) {
    $array[$index] = $phone_number . '@att.com';
}

// join array with a comma
$attusers = implode(',',$array);  

print($attusers); 
Comments