将两个关联数组合并为一个数组 MongoDB Laravel 7
Merge two associative arrays into one array for MongoDB Laravel 7
最终目标:
- 获取旧数组 (repository_data) 及其子数组。
- 在 repository_data 数组中,插入 $newData 数组。
- 将 repository_data 个数组与 newData 数组合并!
我的 MongoDB 上有一个我创建的文档(也许它需要修复?)
当我运行查询结果如下:
#attributes: array: 4[
▼"_id"=>MongoDB\BSON\ObjectId{
#1292▶
}"team_data"=>array: 2[
▼"team_id"=>"94""repository_data"=>array: 1[
▼0=>array: 4[
▼"repository_id"=>2"repository"=>"DataSomething""token"=>"notoken1""url"=>"nourl1"
]
]
]"updated_at"=>MongoDB\BSON\UTCDateTime{
#1293▼+"milliseconds": "1614177748010"
}"created_at"=>MongoDB\BSON\UTCDateTime{
#1294▼+"milliseconds": "1614177748010"
}
]
我感兴趣的是这里存储的值:
team_data['repository_data']
我想做的是创建一个新的 MongoDB 记录,它具有完全相同的格式,但会在“repository_data”中添加另一个数组。
第一次提交的数据是:
array:1 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "ssxc"
"url" => "qwe3"
]
]
这就是我的问题发挥作用的地方:
-> 如何创建一个数组来容纳我的新数组和旧数组(多维数组)?
喜欢将它们合并到一个包含关联数组的大数组中...
-repository_data =>
--[0]RepoData => [Things...]
--[1]RepoData => [Same key things different values]
--[N]RepoData
我想到了遍历旧数组()
Array_merge 不是解决方案。
我的新值存储在以下数组值中:
$newData = array(
'repository_id' => $repositoryID,
'repository' => $request->repository,
'token' => $request->dataverse_token,
'url' => $request->dataverse_url,
);
我想到了这个:
$newData = [
"repository_id" => $repositoryID,
"repository" => $request->repository,
"token" => $request->dataverse_token,
"url" => $request->dataverse_url,
];
$oldData = $collection->team_data['repository_data'];
$arrayPot = array();
foreach ($oldData as $od) {
$oldDataNew= $arrayPot + $od;
}
结果:
array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "WiiVerse"
"token" => "sample1"
"url" => "sample1.com"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "WiiVerse"
"token" => "sample2"
"url" => "sample2.com"
]
]
错误,因为其中一个数组成为 oldDataNew 上的子数组(意味着它们属于 team_data['repository_data'] [0]).
当请求 team_data['repository_data'] 时,我想要以下输出:
array: 5[
▼0=>array: 4[
"repository_id"=>2"repository"=>"wii""token"=>"SomethingElse""url"=>"SE1"
▼1=>array: 4[
"repository_id"=>2"repository"=>"wii""token"=>"ssxc""url"=>"SE1"
]
这会将两个数组合并在一起(理论上?)但是第二个变量的数据将只被存储:
dd(array(($newData)+($oldDataNew)));
如上面的代码所示,这确实(有点)有效:
dd($oldDataNew[]=array(($newData),($oldDataNew)));
BUT 问题在于,当用户尝试输入另一个值(在提交表单时)时,将执行相同的代码。每个旧值都存储在一个大数组中,新值不会插入到该数组中,这是一个问题。它导致 sub sub sub 子数组...
看第三个插入(注意两个旧数组现在是子数组)
0 => array:5 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkkk"
"url" => "llll"
0 => array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "5467"
"url" => "rtf"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkkk"
"url" => "llll"
]
]
]
执行以下操作:
dd($submissionData[]=$oldDataNew+$newData);
结果:
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "5467"
"url" => "rtf"
]
可以看到,丢了一个数组,只存了一个。为什么?因为它们拥有相同的键值?
我越接近(我认为)组合这些数组(旧的和新的)就是通过这个:
$batman = $oldData + $newData;
$oldData 具有以下(例如)值:
array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "fff"
"url" => "vvv"
]
]
$oldData + $newData 将导致:
array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "fff"
"url" => "vvv"
]
]
运行 以下:
foreach (($newData) as $nd){
$oldData= $oldData+ ($nd);
}
将导致:
array:6 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "fff"
"url" => "vvv"
]
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkk"
"url" => "llll"
]
我太天真了:
foreach (($newData) as $nd) {
$arrayPot = array_merge($oldData, array($nd));
}
$newData 在哪里:
$newData = array([
"repository_id" => $repositoryID,
"repository" => $request->repository,
"token" => $request->dataverse_token,
"url" => $request->dataverse_url,
]);
并且 $oldData 保存了 repository_data
之前所有记录的值
我的循环结果如下:
"repository_data" => array:4 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkk"
"url" => "3333"
]
2 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "4444"
"url" => "5555555555"
]
3 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "1111"
"url" => "22333"
]
]
我仍然不明白 $newData 必须是一个数组 然后再次 在我的 foreach 循环中将它声明为一个数组
最终目标:
- 获取旧数组 (repository_data) 及其子数组。
- 在 repository_data 数组中,插入 $newData 数组。
- 将 repository_data 个数组与 newData 数组合并!
我的 MongoDB 上有一个我创建的文档(也许它需要修复?)
当我运行查询结果如下:
#attributes: array: 4[
▼"_id"=>MongoDB\BSON\ObjectId{
#1292▶
}"team_data"=>array: 2[
▼"team_id"=>"94""repository_data"=>array: 1[
▼0=>array: 4[
▼"repository_id"=>2"repository"=>"DataSomething""token"=>"notoken1""url"=>"nourl1"
]
]
]"updated_at"=>MongoDB\BSON\UTCDateTime{
#1293▼+"milliseconds": "1614177748010"
}"created_at"=>MongoDB\BSON\UTCDateTime{
#1294▼+"milliseconds": "1614177748010"
}
]
我感兴趣的是这里存储的值:
team_data['repository_data']
我想做的是创建一个新的 MongoDB 记录,它具有完全相同的格式,但会在“repository_data”中添加另一个数组。
第一次提交的数据是:
array:1 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "ssxc"
"url" => "qwe3"
]
]
这就是我的问题发挥作用的地方: -> 如何创建一个数组来容纳我的新数组和旧数组(多维数组)?
喜欢将它们合并到一个包含关联数组的大数组中...
-repository_data =>
--[0]RepoData => [Things...]
--[1]RepoData => [Same key things different values]
--[N]RepoData
我想到了遍历旧数组() Array_merge 不是解决方案。
我的新值存储在以下数组值中:
$newData = array(
'repository_id' => $repositoryID,
'repository' => $request->repository,
'token' => $request->dataverse_token,
'url' => $request->dataverse_url,
);
我想到了这个:
$newData = [
"repository_id" => $repositoryID,
"repository" => $request->repository,
"token" => $request->dataverse_token,
"url" => $request->dataverse_url,
];
$oldData = $collection->team_data['repository_data'];
$arrayPot = array();
foreach ($oldData as $od) {
$oldDataNew= $arrayPot + $od;
}
结果:
array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "WiiVerse"
"token" => "sample1"
"url" => "sample1.com"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "WiiVerse"
"token" => "sample2"
"url" => "sample2.com"
]
]
错误,因为其中一个数组成为 oldDataNew 上的子数组(意味着它们属于 team_data['repository_data'] [0]).
当请求 team_data['repository_data'] 时,我想要以下输出:
array: 5[
▼0=>array: 4[
"repository_id"=>2"repository"=>"wii""token"=>"SomethingElse""url"=>"SE1"
▼1=>array: 4[
"repository_id"=>2"repository"=>"wii""token"=>"ssxc""url"=>"SE1"
]
这会将两个数组合并在一起(理论上?)但是第二个变量的数据将只被存储:
dd(array(($newData)+($oldDataNew)));
如上面的代码所示,这确实(有点)有效:
dd($oldDataNew[]=array(($newData),($oldDataNew)));
BUT 问题在于,当用户尝试输入另一个值(在提交表单时)时,将执行相同的代码。每个旧值都存储在一个大数组中,新值不会插入到该数组中,这是一个问题。它导致 sub sub sub 子数组...
看第三个插入(注意两个旧数组现在是子数组)
0 => array:5 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkkk"
"url" => "llll"
0 => array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "5467"
"url" => "rtf"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkkk"
"url" => "llll"
]
]
]
执行以下操作:
dd($submissionData[]=$oldDataNew+$newData);
结果:
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "5467"
"url" => "rtf"
]
可以看到,丢了一个数组,只存了一个。为什么?因为它们拥有相同的键值?
我越接近(我认为)组合这些数组(旧的和新的)就是通过这个:
$batman = $oldData + $newData;
$oldData 具有以下(例如)值:
array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "fff"
"url" => "vvv"
]
]
$oldData + $newData 将导致:
array:2 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "fff"
"url" => "vvv"
]
]
运行 以下:
foreach (($newData) as $nd){
$oldData= $oldData+ ($nd);
}
将导致:
array:6 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "fff"
"url" => "vvv"
]
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkk"
"url" => "llll"
]
我太天真了:
foreach (($newData) as $nd) {
$arrayPot = array_merge($oldData, array($nd));
}
$newData 在哪里:
$newData = array([
"repository_id" => $repositoryID,
"repository" => $request->repository,
"token" => $request->dataverse_token,
"url" => $request->dataverse_url,
]);
并且 $oldData 保存了 repository_data
之前所有记录的值我的循环结果如下:
"repository_data" => array:4 [▼
0 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "123sdws"
"url" => "123"
]
1 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "kkkk"
"url" => "3333"
]
2 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "4444"
"url" => "5555555555"
]
3 => array:4 [▼
"repository_id" => 2
"repository" => "dataverse"
"token" => "1111"
"url" => "22333"
]
]
我仍然不明白 $newData 必须是一个数组 然后再次 在我的 foreach 循环中将它声明为一个数组