-2

I have an list of strings as follow:

AA
AB
AC
AD ..

I want to append a specific number to each of my elements from this array. What I want the output to be is

AA12345678
AB12345678 ..

You get the idea. I am a total noob so forgive me. I have tried many codes but I haven't managed to work it out.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
  • Please update your question with the desired behavior, specific problems, and code to reproduce it. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – abestrad Mar 16 '19 at 19:45

2 Answers2

0

You can use the implode function

$str = implode('12345', $values);
jgoday
  • 2,768
  • 1
  • 18
  • 17
0

One of the solutions, if the suffix is always the same, could be as follow:

$a = ['AA','AB', 'AC'];
$suffix = 12345678;

$b = array_map(function($v) use ($suffix) {
    return $v . $suffix;
}, $a);

var_dump($b);

Please see array_map documentation.

Michał Haracewiat
  • 488
  • 1
  • 3
  • 9
  • is there any way to print the output as plain text? without the array [0] ... and so on – Ario Rakip Mar 16 '19 at 19:52
  • Of course. Join my solution with jgoday solution. It will be `echo implode($b)`. If you want a line break between them, use either `echo implode(PHP_EOL, $b)` or `echo implode('
    ', $b)` (if you're printing HTML).
    – Michał Haracewiat Mar 16 '19 at 19:57
  • this worked! I have not created my array of elements, how do I put all the strings (676) into an array? – Ario Rakip Mar 16 '19 at 20:02
  • Sorry, I don't quite get your question. What do you need to put into an array? Permutations of the alphabet letters? – Michał Haracewiat Mar 16 '19 at 20:05
  • yes, I have generated all 2 letter combinations of alphabet letters (AA, BB .. and so on), but they are in plain text and with a line break. How do I put them into an array? Or is there a way I can generate all combinations and append the desired string? – Ario Rakip Mar 16 '19 at 20:07
  • Well, it's a completely different problem that stated in the question. I guess you've created something like the second list from https://en.wikipedia.org/wiki/Wikipedia:List_of_two-letter_combinations, correct? If so, just wrap all pair in a quote, separate them with colon and wrap everything with square brackets (if using PHP 5.4+). Otherwise, you need to look for the algorihm (but I don't think it's worth it) - like here https://stackoverflow.com/questions/20438065/find-all-possible-2-letter-combinations-in-a-given-string-using-php. – Michał Haracewiat Mar 16 '19 at 20:15