我使用 Input::all() 从动态生成的表单中获取一些输入(我使用 jQuery 来允许用户添加字段)。字段名称为“first_names[]”、“last_names[]”和“emails[]”。
$input 变量现在看起来像这样:
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 '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="503a3f383e343f35103528313d203c357e333f3d" rel="noreferrer noopener nofollow">[email protected]</a>' (length=24),
0 => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b414a454e4f444e6b4e534a465b474e05484446" rel="noreferrer noopener nofollow">[email protected]</a>' (length=24)
我想要做的是从该输入创建一个数组,如下所示:
array (
0 => array(
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="610b0e090f050e04210419000c110d044f020e0c" rel="noreferrer noopener nofollow">[email protected]</a>'
),
1 => array(
'first_name' => 'Jane',
'last_name' => 'Doe',
'email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a606b646f646e656f4a6f726b677a666f24696567" rel="noreferrer noopener nofollow">[email protected]</a>'
)
)
有没有一种简单的方法可以做到这一点,而无需迭代每个数组并构建新的数组?有没有更好的方法来生成输入?谢谢。
最佳答案
好的,伙计们,在 Laravel IRC 中 alexrussell 的帮助下,我们已经解决了。
首先是 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();
});
我们创建一个 delegateId 变量,将其附加到获取请求 URL,然后在我们的 paths.php 中执行以下操作:
Route::get('add-delegate/{id}', function($id) {
return View::make('admin.bookings.partials.add-delegate', compact('id'));
});
这会将 id 发送到我们用来生成表单字段的 View 。然后以我们的形式:
<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>
一旦我们使用以下方式获得代表的输入:
Input::get('delegates')
然后我们就有了一个很好的数组可以使用,这正是我们所追求的:
array (size=2)
0 =>
array (size=3)
'first_name' => string 'John' (length=4)
'last_name' => string 'Doe' (length=3)
'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aac0c5c2c4cec5cfeacfd2cbc7dac6cf84c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>' (length=19)
1 =>
array (size=3)
'first_name' => string 'Jane' (length=4)
'last_name' => string 'Doe' (length=3)
'email' => string '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a8a3aca7a6ada782a7baa3afb2aea7eca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>' (length=19)
关于php - 从 Laravel 4 输入生成新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19834200/