如何将数组存储在PHP中由array_chunk拆分的表中? [英] How to store array in table split by array_chunk in php?
问题描述
我正在获取数组格式的数据.有时我会得到重复的数据 在第二个索引之后.所以我决定将数组分为3个索引 php的array_chunk函数.但是在foreach循环上执行此操作后 仅第0个索引.我想将所有0th,1st,2nd ..存储在新表中 行.
I am getting data in array format.Some times i get duplicate data after 2nd index. so i decide to split array in 3 index with array_chunk function of php. But after doing this on foreach loop get only 0th index. i want to store all 0th,1st,2nd.. in tables with new row.
Array
(
[0] => Array
(
[0] => xyz
[1] => dublin
[2] => 2
[3] => yxd
[4] => canada
[5] => 2
)
)
我正在使用array_chunk拆分数组.
I am using array_chunk to split my array.
Array
(
[0] => Array
(
[0] => Suhas shinde
[1] => Jogeshwari
[2] => 2
)
[1] => Array
(
[0] => Suhas shinde
[1] => Jogeshwari
[2] => 2
)
)
现在,我必须将其存储在表中.所以我用了foreach循环:
Now, i have to store this in table. so i used foreach loop :
foreach ($matches as $value)
{
$share_holder_info->trad_id = $rand_id;
$share_holder_info->name = $value[0];
$share_holder_info->address = $value[1];
$share_holder_info->shares = $value[2];
$share_holder_info->save();
}
它仅在表中存储数组的第0个索引.
It stores only 0th index of array in table.
if (preg_match_all('~(?:\G(?!\A)|In respect of Shareholders)\s*[^:\r\n]+:\h*\K.*~', $string, $matches)) {
$matches = array_chunk($matches[0],3);
//foreach loop
}
推荐答案
您需要将foreach更改为此,以便为每个记录集创建新的Model实例.
You need to change your foreach to this to allow for the creation of new Model instance for each record set.
foreach ($matches as $value) {
$share_holder_info = new ShareHolderInfo();
$share_holder_info->trad_id = $rand_id;
$share_holder_info->name = $value[0];
$share_holder_info->address = $value[1];
$share_holder_info->shares = $value[2];
$share_holder_info->save();
}
您需要将其更改为实际模型的名称.
You will need to change it to what ever your actual Model is named.
我希望这对您有帮助
这篇关于如何将数组存储在PHP中由array_chunk拆分的表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!