1

I would like to combine 2 arrays into 1 in PHP or laravel. 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 **

Array
(
[0] => Array
    (
        [prid] => 4
        [vendor_id] => 1
    )

[1] => Array
    (
        [prid] => 5
        [vendor_id] => 2
    )

)

**Array - 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 **

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

    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);
}
Prashant More
  • 21
  • 1
  • 4

3 Answers3

0

you can achieve this using the array_combine function in php, for example:

<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");

$c=array_combine($fname,$age);
print_r($c);
?>
Rian Zaman
  • 409
  • 1
  • 6
  • 18
0

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).

Assuming that the vendor_id values in the first array are unique, you will get best performance by creating a lookup array. array_column() can be used to declare an array with vendor_id values as keys and prid values as values.

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.

Code: (Demo)

$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:

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,
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

You can use the array_merge() function to merge arrays. array_merge

$merged_array = array_merge($insertData, $requestData);
Rian Zaman
  • 409
  • 1
  • 6
  • 18