从Laravel 4输入产生新阵列 [英] Generating New Array from Laravel 4 Input

查看:104
本文介绍了从Laravel 4输入产生新阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到从动态生成的表单输入使用(我使用jQuery来允许用户添加字段)::一些输入所有的()。字段名first_names []','last_names []和电子邮件[]'。

I'm getting some input from a dynamically generated form (I'm using jQuery to allow the user to add fields) using Input::all(). The field names are 'first_names[]', 'last_names[]' and 'emails[]'.

在$输入变量现在看起来是这样的:

The $input variable now looks like this:

array (size=4)
  '_token' => string '3VCQFUmAx8BNbSrX9MqjGtQhYovOaqecRUQSAL2c' (length=40)
  'first_names' => 
    array (size=1)
      0 => string 'John' (length=4),
      1 => string 'Jane' (length=4)
  'last_names' => 
    array (size=1)
      0 => string 'Doe' (length=3),
      1 => string 'Doe' (length=3)
  'emails' => 
    array (size=1)
      0 => string 'johndoe@example.com' (length=24),
      0 => string 'janedoe@example.com' (length=24)

我想要做的就是创建从输入,看起来像这样的数组:

What I want to do is create an array from that input that looks like this:

array (
    0 => array(
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => 'johndoe@example.com'
    ),
    1 => array(
        'first_name' => 'Jane',
        'last_name' => 'Doe',
        'email' => 'janendoe@example.com'
    )
)

有一个简单的方法来做到这一点没有遍历每个阵列和建设新的?有没有更好的方式来生成输入?谢谢你。

Is there a simple way to do this without iterating over each array and building new ones? Is there a better way to generate the input? Thanks.

推荐答案

确定的家伙,用alexrussell在Laravel IRC的帮助下,我们已经理解了它。

OK guys, with the help of alexrussell in Laravel IRC we've figured it out.

第一件事是JS:

var delegateId = 0;

$('.add-delegate').click(function() {
    $.get('/add-delegate/' + delegateId++, function(html) {
        $(html).appendTo('#delegates');
    });
});

$(document).on('click', '.remove-delegate', function() {
    $(this).parent().parent().remove();
});

我们创造,我们追加到GET请求的URL,然后在我们的routes.php文件,我们做一个delegateId变量:

We create a delegateId variable which we append to the get request URL and then in our routes.php we do:

Route::get('add-delegate/{id}', function($id) {
    return View::make('admin.bookings.partials.add-delegate', compact('id'));
});

这将该ID送到我们用来生成表单域视图。然后在表格我们做的:

This sends the id to the view we use to generate the form fields. Then in the form we do:

<div class="row-fluid">
    <div class="span12">
        <input type="text" name="delegates[{{ $id }}][first_name]" placeholder="First Name">
        <input type="text" name="delegates[{{ $id }}][last_name]" placeholder="Last Name">
        <input type="text" name="delegates[{{ $id }}][email]" placeholder="Email">
        <label class="checkbox">
            <input type="checkbox" name="delegates[{{ $id }}][prerequisites]"> Prerequisites
        </label>
        <button type="button" class="btn btn-danger remove-delegate">Remove</button>
    </div>
</div>

一旦我们得到的输入使用委托:

Once we get the input for delegates using:

Input::get('delegates')

我们再有一个很好的阵列一起工作,这正是我们所追求的:

We then have a nice array to work with, that is exactly what we're after:

array (size=2)
  0 => 
    array (size=3)
      'first_name' => string 'John' (length=4)
      'last_name' => string 'Doe' (length=3)
      'email' => string 'johndoe@example.com' (length=19)
  1 => 
    array (size=3)
      'first_name' => string 'Jane' (length=4)
      'last_name' => string 'Doe' (length=3)
      'email' => string 'janedoe@example.com' (length=19)

这篇关于从Laravel 4输入产生新阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆