0

I need to merge arrays into 1 array but what I need is to remove before the main data they both have in common (duplicated values I mean), I need only unique values when merged. array formed from foreach.

 public $arrayfields= [];
    public $itemMap = [];

    public function getRowMapping(array $row,$uniqueField,$moduleName)
    { 
        $arrayData = []; 
        foreach ($this->columnMapping as $key => $value) {
            $row = $this->moduleType($key,$value,$row,$moduleName); 
        } 
        $unique = $row[$uniqueField];
        if(!isset($this->itemMap[$unique])){   
          $this->itemMap[$unique] = $row; 
          $this->itemMap[$unique][$unique]['items'] = [];
        }      
        $this->itemMap[$unique]['items'][] = $row['items'];   
        return $row;
     
    }

I changed little bit to my input How can I do that? so now the input will be like this

Array
(
    [bill_type] => 1
    [bill_number] => BIL-003
    [order_number] => ON-003
    [items] => Array
        (
            [track_inventory] => 0
            [sku1] => sku1
            [name] => Kidswear1
        )

)
Array
(
    [bill_type] => 1
    [bill_number] => BIL-003
    [order_number] => ON-003
    [items] => Array
        (
            [track_inventory] => 0
            [sku1] => sku2
            [name] => Pant
        )

)
Array
(
    [bill_type] => 1
    [bill_number] => BIL-002
    [order_number] => ON-002
    [items] => Array
        (
            [track_inventory] => 0
            [sku1] => sku3
            [name] => Pants
        )

)

The final output I'm looking for is

Array
(
[BIL-003] => Array
(
    [bill_type] => 1    
    [order_number] => ON-003
    [items] => Array
        (
          [0] => Array(
            [track_inventory] => 0
            [sku1] => sku1
            [name] => Kidswear1
        )

    [1] => Array
        (
            [track_inventory] => 0
            [sku1] => sku2
            [name] => Pant
        )

)

[BIL-002] => Array
(
    [bill_type] => 1    
    [order_number] => ON-002
    [items] => Array
        (
          [0] => Array(
            [track_inventory] => 0
            [sku1] => sku3
            [name] => pants
        )


)
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Do any of these help? [PHP - How to merge arrays inside array](https://stackoverflow.com/questions/17041278/php-how-to-merge-arrays-inside-array), [How to merge or combine 2 arrays based on their keys in php](https://stackoverflow.com/questions/38374087/how-to-merge-or-combine-2-arrays-based-on-their-keys-in-php), [Merging arrays based on a value of the key](https://stackoverflow.com/questions/38396616/merging-arrays-based-on-a-value-of-the-key) – Definitely not Rafal Feb 18 '21 at 11:45
  • Input will be array of ascociative array right? or it will be 2 or more arrays? because from your input snippet it looks like you want 2 diff arrays as input – Rudra Feb 18 '21 at 12:11

4 Answers4

3

I think there is no standard library function to accomplish this.

So here is the function that accomplishes this:

function merge($array1, $array2)
{
    $arraymerged = [];
    if (is_array($array1) && is_array($array2)) {
        //collect the keys of two arrays
        $keys = [];
        foreach ($array1 as $key => $value) {
            $keys[$key] = true;
        }
        foreach ($array2 as $key => $value) {
            $keys[$key] = true;
        }
        //merge key values for each key
        foreach ($keys as $key => $value) {
            if (isset($array1[$key]) && isset($array2[$key])) {
                if ($array1[$key] == $array2[$key]) {
                    $arraymerged[$key] = $array1[$key];
                } else {
                    $arraymerged[$key] = [$array1[$key], $array2[$key]];
                }
            } else if (isset($array1[$key]))
                $arraymerged[$key] = $array1[$key];
            else
                $arraymerged[$key] = $array2[$key];
        }
        return $arraymerged;
    } 
}
Naz
  • 31
  • 3
0

Here is my suggestion, if the input is array, check my answer on this post Convert an associative array into an associative array which has a key with another associative array as its value in php

otherwise check below code.

bill_number as unique key and run foreach and append items array elements. let me give you an example

$arr1 = [
    "bill_type" => 1,
    "bill_number" => 'BIL-003',
    "items"=>[
       0 => [
         "name"=>"test"
       ]
     ]
];

$arr2 = [
    "bill_type" => 1,
    "bill_number" => 'BIL-003',
    "items"=>[
       0 => [
         "name"=>"test_2"
       ]
     ]
];
$formattedArr = [];

 //merge these arrays
$combinedArr = [];
$combinedArr[] = $arr1;
$combinedArr[] = $arr2;
foreach($combinedArr as $key=>$value){
   $formattedArr[$value['bill_number']]["bill_type"] = $value["bill_type"];
   $formattedArr[$value['bill_number']]["items"][] = $value["items"]; 
}
print_r($formattedArr);

I haven't tested this code, but i think this will be ans to your question.

Rudra
  • 704
  • 8
  • 16
  • @PHPCodefinder-fresher Can you send me your code? and test data? Will you have multiple associative arrays as input? – Rudra Feb 23 '21 at 12:05
0
<?php

$a1 = array(
    "bill_type" => "1",
    "bill_number" => "BIL-003",
    "order_number" => "ON-003",
    "items" => array(
        "track_inventory" => "0",
        "sku1" => "sku1",
        "name" => "Kidswear1"
    )
);

$a2 = array(
    "bill_type" => "1",
    "bill_number" => "BIL-003",
    "order_number" => "ON-003",
    "items" => array(
        "track_inventory" => "0",
        "sku1" => "sku2",
        "name" => "Pant"
    )
);

$result = function ($a1, $a2) {
    $b1 = $a1['items'];
    $b2 = $a2['items'];

    $c1 = $a1;
    $c2 = $a2;
    unset($c1['items']);
    unset($c2['items']);

    if (count(array_diff($c1, $c2)) == 0) {
        if (count(array_diff($b1, $b2)) != 0) {
            $c1['items'] = [$b1, $b2];
        } else {
            $c1['items'] = $b1;
        }

        return $c1;
    }else{
        //you should complete this part by yourself, if first parts are not equal.
    }

    return $c1;
};

$r = $result($a1, $a2);

var_dump($r);

result:

(array) [4 elements]
bill_type: (string) "1"
bill_number: (string) "BIL-003"
order_number: (string) "ON-003"
items: 
(array) [2 elements]
0: 
(array) [3 elements]
track_inventory: (string) "0"
sku1: (string) "sku1"
name: (string) "Kidswear1"
1: 
(array) [3 elements]
track_inventory: (string) "0"
sku1: (string) "sku2"
name: (string) "Pant"
Raskul
  • 1,674
  • 10
  • 26
0
  1. Gather your input arrays into an array and use a single loop to iterate each array.
  2. Assign temporary keys when pushing data into the result array.
  3. The first encountered items data needs to be pushed into a deeper level so that all pushed items data is indexed as desired.

Code: (Demo)

$result = [];
foreach ([$array1, $array2, $array3] as $row) {
    $key = $row['bill_number'];
    if (!isset($result[$key])) {
        $row['items'] = [$row['items']]; // push associative elements lower in the element
        $result[$key] = $row;
    } else {
        $result[$key]['items'][] = $row['items']; // only push items data into group's subarray
    }
}
var_export($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136