简体   繁体   English

我想将名称、email、联系人的 0 索引存储在新数组中,反之亦然所有数据

[英]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);

https://3v4l.org/DCP5W https://3v4l.org/DCP5W

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除所有外键数据,删除主数据或反之亦然 - Delete all foreign key data on delete of primary or vice-a-versa 如何将数据从JavaScript发送到PHP,反之亦然? - How to I send data from JavaScript to PHP and vice versa? 我想将所有大写 url 重定向到小写,反之亦然,在 godaddy 托管的 vps linux 大约有 300 个 url。 - I want to redirect all upper-case url to lower-case and vice versa at vps linux hosting at godaddy approx 300 urls. 我想将图像路径存储在带有索引的数组中,因为用户在 codeigniter 中发送电子邮件 - I want to store image path in array with index as users email in codeigniter 我想在不使用任何包的情况下使用 php 将回历日期转换为公历日期,反之亦然 - I want to convert hijri date to gregorian date vice versa using php without using any packages 将数据从PHP传递到Javascript和Vice Versa - Pass Data from PHP to Javascript and Vice Versa 在PHP和Wordpress之间传递数据,反之亦然 - Passing data between PHP and Wordpress and vice versa 如何获取数据库中字段名(名称)中的所有数据并将其存储在变量数组中? 在 PHP 和 MYSQL - HOw do i get all the data in a fieldname (Name) in database and store it in a variable array? in PHP and MYSQL 如何在PHP中从时区名称转换为时区偏移量,反之亦然 - How to convert from timezone name to timezone offset and vice versa in PHP 从JavaScript发送二维数组到php,反之亦然 - sending a two dimension array from JavaScript to php and vice versa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM