Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

[email protected],[email protected],9898549132

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

Expected output:

[email protected],[email protected],[email protected]
                                                      //^^^^^^^^^^^^ See here
like image 465
Ryan Litwiller Avatar asked Sep 01 '15 16:09

Ryan Litwiller


1 Answers

There is a simpler, better, more efficient way to achieve this using array_map and a lambda function:

$numbers = ['9898549130', '9898549131', '9898549132'];

$attUsers = implode(
    ',',
    array_map(
        function($number) {
            return($number . '@txt.att.net');
        },
        $numbers
    )
);

print_r($attUsers);
like image 187
aalaap Avatar answered Sep 19 '22 18:09

aalaap