[英]How can I combine 2 array in php or laravel
I would like to combine 2 arrays into 1 in PHP or laravel.我想在 PHP 或 laravel 中将 2 个 arrays 合并为 1 个。 I've searched this site for similar questions but can't seem to find an answer.我在这个网站上搜索过类似的问题,但似乎找不到答案。 Can someone help me with this?有人可以帮我弄这个吗?
**array 1 -- $insertData ** **数组 1 -- $insertData **
Array
(
[0] => Array
(
[prid] => 4
[vendor_id] => 1
)
[1] => Array
(
[prid] => 5
[vendor_id] => 2
)
)
**Array - 2 $requestData ** **数组 - 2 $requestData **
Array
(
[vendor_id] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
)
[item] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
[qty] => Array
(
[0] => 12
[1] => 13
[2] => 14
[3] => 15
)
)
**Required Output ---- how can I do this array1 and array2 combine into a single array ** **必需的 Output ----我怎样才能将这个 array1 和 array2 组合成一个数组**
Array
(
[0] => Array
(
[prid] => 4
[vendor_id] => 1
[item] => 2
[qty] => 12
)
[1] => Array
(
[prid] => 4
[vendor_id] => 1
[item] => 3
[qty] => 13
)
[2] => Array
(
[prid] => 5
[vendor_id] => 2
[item] => 4
[qty] => 14
)
[3] => Array
(
[prid] => 5
[vendor_id] => 2
[item] => 5
[qty] => 15
)
)
My controller我的 controller
public function prtmulti(Request $req)
{
$maxPrId = newpr::max('prid');
// print_r($maxPrId);
echo "<pre>";
$requestData = $req->all();
if (array_key_exists("vendor_name", $requestData)) {
$insertData = [];
$uniqueData = array_unique($requestData["vendor_name"]);
foreach ($uniqueData as $key => $value) {
$maxId = $maxPrId+1;
$insertData[] = ['prid' => $maxId, 'vendor_id' => $value];
$maxPrId = $maxPrId+1;
}
}
print_r($insertData);
print_r($requestData);
}
you can achieve this using the array_combine
function in php, for example:您可以使用array_combine
中的 array_combine function 来实现此目的,例如:
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
I'm pretty sure that Laravel doesn't offer anything out of the box to execute your desired merging technique (and I don't see why it would bother).我很确定 Laravel 没有提供开箱即用的任何东西来执行您想要的合并技术(我不明白它为什么会打扰)。
Assuming that the vendor_id
values in the first array are unique, you will get best performance by creating a lookup array.假设第一个数组中的vendor_id
值是唯一的,您将通过创建查找数组获得最佳性能。 array_column()
can be used to declare an array with vendor_id
values as keys and prid
values as values. array_column()
可用于声明一个数组,其中vendor_id
值作为键, prid
值作为值。
Because your $requestData
has rows with the number of columns desired in the output, loop over the $requestData['vendor_id']
data and manually generate the desired rows of data in the result array.因为您的$requestData
的行具有 output 中所需的列数,所以循环$requestData['vendor_id']
数据并在结果数组中手动生成所需的数据行。
$insertData = [
['prid' => 4, 'vendor_id' => 1],
['prid' => 5, 'vendor_id' => 2],
];
$requestData = [
'vendor_id' => [1, 1, 2, 2],
'item' => [2, 3, 4, 5],
'qty' => [12, 13, 14, 15]
];
$insertLookup = array_column($insertData, 'prid', 'vendor_id');
$result = [];
foreach ($requestData['vendor_id'] as $index => $vendorId) {
$result[] = [
'prid' => $insertLookup[$vendorId],
'vendor_id' => $vendorId,
'item' => $requestData['item'][$index],
'qty' => $requestData['qty'][$index],
];
}
var_export($result);
Output: Output:
array (
0 =>
array (
'prid' => 4,
'vendor_id' => 1,
'item' => 2,
'qty' => 12,
),
1 =>
array (
'prid' => 4,
'vendor_id' => 1,
'item' => 3,
'qty' => 13,
),
2 =>
array (
'prid' => 5,
'vendor_id' => 2,
'item' => 4,
'qty' => 14,
),
3 =>
array (
'prid' => 5,
'vendor_id' => 2,
'item' => 5,
'qty' => 15,
),
)
You can use the array_merge()
function to merge arrays.您可以使用array_merge()
function 来合并 arrays。 array_merge数组合并
$merged_array = array_merge($insertData, $requestData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.