在laravel表单验证,验证数组中,怎么去获取数组的索引


提交的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"items": [
{
"product_id": "1",
"sku_id": "1",
"amount": 2
},
{
"product_id": "2",
"sku_id": "2",
"amount": 3
}
]
}

php验证

1
2
3
4
5
6
7
8
9
10
11
12
'items' => ['required', 'array'],
'items.*.product.*.product_id' => [
'required',
function ($attribute, $value, $fail) {
// 获取items下索引
preg_match('/items\.(\d+)\.product/', $attribute, $m);
//获取product下索引
$sku = substr ($attribute, 0,strlen($attribute)-10).'sku_id';
preg_match('/items\.'.$m[1].'\.product\.(\d+)\.sku_id/', $sku, $s);
}

...