我有一个数组$ subCatArray
$subCatArray = [
'Category1' => [
1 => 'product 1',
2 => 'product 2'
],
'Category2' => [
4 => 'product 3',
5 => 'product 4'
]
]
并选择为:
{!! Form::select('productselect', $subCatArray, null,
['class' => 'form-control 'data-placeholder' => 'Select Product']) !!}
我在我的html中得到了这个
<option value="1">product 1</option>
我希望它是<option value="product 1">product 1</option>
该选项的值应为数组值,而不是数组键
答案 0 :(得分:1)
在这种情况下,您必须更新数组,以使key
和value
相等。像这样:
$subCatArray = [
'Category1' => [
'product 1' => 'product 1',
'product 2' => 'product 2'
],
'Category2' => [
'product 3' => 'product 3',
'product 4' => 'product 4'
]
]
表单助手使用数组键作为选项值,并使用数组值作为选项文本。
答案 1 :(得分:0)
我不认为laravelcollective有办法,如果要保持数组结构,则需要使用html进行选择,并在其中使用foreach进行循环,并在option标记和int之间打印两次值。价值属性。
答案 2 :(得分:0)
使用array_get检索您的数组值
$subCatArray = [
'Category1' => ['product1' => 'product 1', 'product2' => 'product 2'],
'Category2' => ['product3' => 'product 3', 'product4' => 'product 4']
];
$value = array_get($subCatArray, 'Category1.product1');
return $value;
答案 3 :(得分:-1)
检索数组以传递以在下拉列表中查看 这是laravel路线
Route::get('/calstack', function (){
$subCatArray = array([
'Category1' => [
1 => 'This is oneof product',
2 => 'this is product2'
],
'Category2' => [
4 => 'product 3',
5 => 'product 4'
]
]);
foreach($subCatArray as $sub){
$catefoory =$sub['Category1'];
$catefoory2 =$sub['Category2'];
}
$array_cat_value = array(
'Categories1'=>$catefoory,
'Categories2'=>$catefoory2
);
return view('welcome', $array_cat_value);
});
这是查看文件
<div class="container">
<div class="content">
<select>
@foreach($Categories1 as $pro)
<option value="{{$pro}}"> {{$pro }} <br></option>
@endforeach
</select>
</div>
</div>