[英]I want to store the 0 indexes of name,email,contact in new array and vice versa all data
My result is that the appended data look like this on form request in laravel我的结果是附加数据在 laravel 中的表单请求中看起来像这样
I want this type of array in my result the first data of append block store in single index and then vice versa all data我希望这种类型的数组在我的结果中 append 块的第一个数据存储在单个索引中,反之亦然所有数据
name=>basit
email=>kashif@gmail.com
contact=>5454
])
sub_contact => array([
name=>basit
email=>hamza12433@gmail.com
contact=>53543
])
You don't need to change your request array structure.您无需更改请求数组结构。 Just loop through an element and insert values into database.只需循环一个元素并将值插入数据库。
foreach($request->name as $key => $value){
Model::create([
'name' => $request->name[$key],
'email' => $request->email[$key],
'contact' => $request->contact[$key],
]);
}
You can loop one of the subarrays and use the key in a array_column to get all the values.您可以循环其中一个子数组并使用 array_column 中的键来获取所有值。
$keys = array_keys($arr);
foreach($arr[$keys[0]] as $index => $value){
$result[] = array_combine($keys, array_column($arr, $index));
}
var_dump($result);
https://3v4l.org/kUPi4 https://3v4l.org/kUPi4
Or nest the foreach to get the values that way.或者嵌套 foreach 以这种方式获取值。
I believe this method is faster than array_column one.我相信这种方法比 array_column 更快。
foreach($arr as $key => $value){
foreach($value as $index => $item){
$result[$index][$key] = $item;
}
}
var_dump($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.