0

I have a design like this,

enter image description here

In controller I am having this code,

$initials   =  $request->customer_initial;
$firstnames =  $request->room_customerfirstname;
$lastnames  =  $request->room_customerlastname;

print_r($initials);
print_r($firstnames);
print_r($lastnames);

It give result as below,

Array
(
    [0] => Mr
    [1] => Ms
)
Array
(
    [0] => Jeffrey
    [1] => Taylor
)
Array
(
    [0] => Way
    [1] => Otwell
)

But what i want is like this in array format or collection format:

Mr Jeffrey Way
Mr Taylor Otwell

How can I get result like this?

M Reza
  • 18,350
  • 14
  • 66
  • 71
arun
  • 4,595
  • 5
  • 19
  • 39

1 Answers1

2

Not everyone knows but array_map has a feature:

// pass NULL as a callback and
print_r(array_map(null, $initials, $firstnames, $lastnames));

Another approach is to rename fields on your form, so as every group of fields describes a person.

<input name="customer[1][initials]" />
<input name="customer[1][first_name]" />
<input name="customer[1][last_name]" />

<input name="customer[2][initials]" />
<input name="customer[2][first_name]" />
<input name="customer[2][last_name]" />

// etc.

But in this case you need to control indexes 1,2,3.. manually, because just adding [] will not work.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • 1
    To expand on that, to get the full names using collections, OP could do e.g. `collect(array_map(...))->map->implode(' ')` – Joel Hinz May 26 '18 at 09:57