0

I need to reverse the orientation of the first name and last name in an array of name strings.

$names = [
    "barira, stefan",
    "cikka, maria",
    "luppo, martin ",
    "bill, sebastian"
];

the output must be like this:

Array ( [0] => stefan, barira 
        [1] => maria, cikka 
        [2] => martin, luppo  
        [3] => sebastian, bill ) 

I got close with this code, but I want it to be like the output above:

$names = [
    "barira, stefan",
    "cikka, maria",
    "luppo, martin ",
    "bill, sebastian"
];
 
for ($i = 0; $i < count($names); $i++) {
    $words = $names[$i];
    $string = explode(' ', $words);
    $string = array_reverse($string);
    $reverse = implode(' ', $string);
    print_r($reverse);
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
omar.f
  • 3
  • 1
  • `', '`, not `' '` in your explode/implode. – Sammitch Feb 07 '19 at 19:13
  • its probably a bad idea keeping that `,` in there. How will know it's been "reversed". Also the standard way is `First Middle Last` OR `Last, First Middle` – ArtisticPhoenix Feb 07 '19 at 19:14
  • `$item = implode(' ', array_reverse(array_filter(array_map('trim', explode(',', $item)))))` - that is what I would do, of course you'll have to loop over the names array. [Sandbox](http://sandbox.onlinephpfunctions.com/code/0e9bc3dd01101af5d4863fd9d4586a1787bc6b26) – ArtisticPhoenix Feb 07 '19 at 19:16

3 Answers3

1

This is what I would do

$names = ["barira, stefan",
      "cikka, maria",
      "luppo, martin ",
      "bill, sebastian"];

//note the & pass by reference, this way we don't even need a new array
foreach($names as &$name)
  $name= implode(' ', array_reverse(array_filter(array_map('trim', explode(',', $name)))));

print_r($names);

Output

Array
(
    [0] => stefan barira
    [1] => maria cikka
    [2] => martin luppo
    [3] => sebastian bill
)

Sandbox

I can explain what these functions do if you want...

If you absolutely want that , back in there, which I think is a bad idea, you can just change this:

 $name = implode(', ', ...... );

From a comment I made: it's probably a bad idea keeping that , in there. How will know it's been "reversed". Also the standard way is First Middle Last OR Last, First Middle

PS. you can add ucwords() to uppercase the first letter of the names. Just wrap it around the implode, or before it's exploded

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • only a sidenote to _"the standard way is First Middle Last"_ (not ment to be correcting, but enhancing for future readers): it's the standard in most indo-eruopean languages, but can be quite different already in some other languages in the middle of europe, not to speak of the austroasian world. So better not rely or restrict to that standard. – Jeff Feb 07 '19 at 20:23
  • @Jeff - that was why I included an example of how to add it in. Useful to know though. I typically deal only with the US (because of my work, B2B). – ArtisticPhoenix Feb 07 '19 at 20:37
  • hi thank you for your answer, it dosn't work it gives only the last value which is bill sebastian it dosn't run the loop – omar.f Feb 11 '19 at 18:39
  • Well it does work, you can see this in the sandbox. I am not sure what you have done, but what I put above certainly works, or I wouldn't have posted the sandbox link. – ArtisticPhoenix Feb 11 '19 at 18:46
  • Sorry, Yes it works i tried again thank You very much for your help. – omar.f Feb 12 '19 at 15:20
0

Here's two fun ways. Map each element, explode and concatenate in reverse:

$result = array_map(function($v) {
                        $a = explode(',', $v);
                        return trim($a[1]).', '.trim($a[0]);
                    }, $names);

Example 1

Or, capture the two names and replace with them reversed:

$result = preg_replace('/(\w+),\s+(\w+)/', '$2, $1', $names);

Example 2

  • Match word characters \w one or more times + and capture in group 1 ()
  • Followed by a , and then space \s one or more times +
  • Followed by word characters \w one or more times + and capture in group 2 ()
  • Replace with capture group 2 a , and a space and capture group 1 $2, $1
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Thanks for the answer but it doesn't work i tried, i got nothing just blank. – omar.f Feb 11 '19 at 18:43
  • Then you did something else wrong. I added example links, works as it should. – AbraCadaver Feb 11 '19 at 18:49
  • Yes You have right i did sth wrong both examples work, can You pls explain the seconde example i don't i understand the signs inside preg_replace() – omar.f Feb 12 '19 at 16:27
  • Added a little description, you can check here to learn https://www.regular-expressions.info/ and here to test https://regex101.com/ – AbraCadaver Feb 12 '19 at 16:34
0

One preg_replace() call will be enough to reliably trim unwanted characters and move last names after first names.

Code: (Demo)

$names = [
    " barira, stefan",
    "cikka , maria",
    "luppo, martin ",
    "bill,sebastian",
    "van gogh , mary jane",
];

var_export(
    preg_replace('~^\s*([^,]+?)\s*,\s*(.*?)\s*$~', '$2 $1', $names)
);

Output:

array (
  0 => 'stefan barira',
  1 => 'maria cikka',
  2 => 'martin luppo',
  3 => 'sebastian bill',
  4 => 'mary jane van gogh',
)

The pattern will:

  1. match leading whitespace characters from the start of the string
  2. lazily capture non-comma characters
  3. match whitespace characters before the comma
  4. match the comma
  5. match whitespace characters after the comma
  6. lazily capture one or more of any character
  7. match whitespace characters at the end of the string
mickmackusa
  • 43,625
  • 12
  • 83
  • 136