代码之家  ›  专栏  ›  技术社区  ›  arun

通过一组输入数组的Laravel循环

  •  0
  • arun  · 技术社区  · 8 年前

    我有这样的设计,

    enter image description here

    在控制器中,我有这个代码,

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

    结果如下:

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

    但我想要的是这样的数组格式或集合格式:

    Mr Jeffrey Way
    Mr Taylor Otwell
    

    我怎样才能得到这样的结果?

    1 回复  |  直到 8 年前
        1
  •  2
  •   u_mulder    8 年前

    不是每个人都知道,但是 array_map 具有以下功能:

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

    另一种方法是重命名表单上的字段,以便每组字段都描述一个人。

    <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.
    

    但在这种情况下,您需要控制索引 1,2,3.. 手动,因为只需添加 [] 不起作用。