laravel 按共享列合并单独的数组,并在组内创建索引子数组

hmae6n7t  于 2023-03-04  发布在  其他
关注(0)|答案(4)|浏览(112)

我需要将数组合并成1个数组,但我需要的是删除它们共同的主要数据(我的意思是重复的值),我只需要合并时的唯一值。

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;
     
    }

我对我的输入做了一点修改我该怎么做?所以现在输入将是这样的

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
        )

)

我要的最终输出是

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
        )

)
)
kpbpu008

kpbpu0081#

我认为没有标准的库函数来实现这一点。
下面是实现这一点的函数:

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;
    } 
}
7jmck4yq

7jmck4yq2#

这里是我的建议,如果输入是数组,检查我的回答在这篇文章中转换一个关联数组到一个关联数组,其中有一个键与另一个关联数组作为其值在php
否则检查以下代码。
bill_number作为唯一键,运行foreach并追加items数组元素。让我给予你一个例子

$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);

我还没有测试这个代码,但我认为这将是一个你的问题。

92vpleto

92vpleto3#

<?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);

结果:

(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"
ne5o7dgx

ne5o7dgx4#

1.将输入数组聚集成一个数组,并使用单个循环迭代每个数组。
1.在将数据推送到结果数组中时分配临时键。
1.第一次遇到的items数据需要被推入更深的级别,以便所有被推入的items数据被按需索引。
代码:(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);

相关问题