0

I was fortunate enough to receive this code (flips Lastname, Firstname) from an earlier post.

$name = "Lastname, Firstname";
$names = explode(", ", $name);
$name = $names[1] . " " . $names[0];

How do I apply the function to each value in an array that is in the form: $ginfo ->$(LastName, FirstName).

I tried the code below, but it doesn't work.

$name1 =($ginfo->White); 
$name1 = explode(", ", $name1);  $FLw = $name1[1] . " " . $name1[0]; 
foreach ($name1 as ($ginfo->White)) {return($FLw);}

1 Answer 1

4

Use the array_map function:

function transpose($name)
{
    $names = explode(", ", $name);
    return $names[1] . " " . $names[0];
}

$transposed_array = array_map("transpose", $your_array);
Sign up to request clarification or add additional context in comments.

3 Comments

+1, removed my answer, there wasn't any difference with mine.
Thanks Andrew. I've been sweating this one out for a while and couldn't figure it out.
The value I have for "$your_array" as written in the code is a table column ($ginfo->$names). I can't find a way to include it in the syntax above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.