I have two fields
- first_name
- last_name
Both are arrays.
I would like to combine the arrays and covert to a human readable string. How can I achieve this?
Here is where I’m at:
$firstName = $this->state['first_name']; $lastName = $this->state['last_name']; $firstLastName = array_combine($firstName , $lastName); /* $firstLastName outputs: array:2 [▼ "John" => "Doe" "Jane" => "Doe" ] */ $string = implode(', ', $firstLastName); dd($string); // $string outputs: //"Doe, Doe" //Only getting the last names
How can I get this to output John Doe, Jane Doe
?
Advertisement
Answer
Use array_map to combine arrays
$firstName = $this->state['first_name']; $lastName = $this->state['last_name']; $firstLastName = array_map(function($f, $l) { return $f.' '.$l;}, $firstName, $lastName);