【问题标题】:How to merge two arrays diferents on one如何将两个不同的数组合并到一个上
【发布时间】:2021-02-09 19:42:07
【问题描述】:

如何更新对象数组,如果您已经有相同的 ID,或者如果您还没有创建新对象,则添加数量。

我尝试在代码中解释数组以及我希望结果如何的想法。

旧数组

$a1 = [
    array(
        "id" => 1,
        "qty" => 1
    ),
    array(
        "id" => 2,
        "qty" => 1
    )
];

$a2 = [
    array(
        "id" => 1,
        "qty" => 1
    )
];

$output = array_merge($a1, $a2);

echo '<pre>';
print_r($output);
echo '</pre>';

结果错误:

Array
(
    [0] => Array
        (
            [id] => 1
            [qty] => 1
        )

    [1] => Array
        (
            [id] => 2
            [qty] => 1
        )

    [2] => Array
        (
            [id] => 1
            [qty] => 1
        )

)

我需要的,除了如果ID不包含,添加。

Array
(
    [0] => Array
        (
            [id] => 1
            [qty] => 2
        )

    [1] => Array
        (
            [id] => 2
            [qty] => 1
        )   
)

【问题讨论】:

  • 你想把它编码成json格式吗?如是?那么这可以帮助你:stackoverflow.com/a/66077071/7698734
  • @HassaanAli 不,结果是 php。
  • 你想要纯 php 的结果吗?没有json?
  • @HassaanAli 是在 php 数组中,我需要在 Wordpress 中记录。
  • 使用php函数数组合并函数来合并它们。这是给你的示例代码。 ` ` 这将以单数组格式打印两个数组。

标签: php


【解决方案1】:

您可以将第一个数组作为基数,然后搜索productid 匹配的键(如果存在)。然后要么添加数量并重新计算价格,要么只添加重新格式化的元素(idproduct 转换)。

$result = $a;
foreach($b as $element) {
    $matchingProductIndex = array_search($element['id'], array_column($a, 'product'));
    if ($matchingProductIndex !== false) {
        $pricePerUnit = $result[$matchingProductIndex]['price'] / $result[$matchingProductIndex]['qty'];
        $result[$matchingProductIndex]['qty'] += $element['qty'];
        $result[$matchingProductIndex]['price'] = $result[$matchingProductIndex]['qty'] * $pricePerUnit;
    } else {
        $result[] = [
            'qty' => $element['qty'],
            'product' => $element['id'],
            'price' => $element['price'],
        ];
    }
}

print_r($result);

工作example

【讨论】:

  • 顶非常非常感谢
【解决方案2】:

使用 foreach 遍历两个数组并检查 id。 https://paiza.io/projects/lnnl5HeJSFIOz_6KD6HRIw

<?php

$arr1 = [['qty' => 4, 'id' => 4],['qty' => 1,'id' => 30]];
$arr2 = [['id' => 30, 'qty' => 19],['id' => 31, 'qty' => 2]];
$arr3 = [];
foreach($arr1 as $iArr1){
    $match = false;
    foreach($arr2 as $iArr2){
        if($iArr1['id'] === $iArr2['id']){
           $arr3[] = ['id' => $iArr1['id'], 'qty' => $iArr1['qty'] + $iArr2['qty']];
           $match = true;
        }
    }
    if(!$match){
        $arr3[] = $iArr1;
        $arr3[] = $iArr2;
    }
}

print_r($arr3);
?>

【讨论】:

    【解决方案3】:

    一种方法可能是我更经常建议的方法。 首先让我们将$a2 与一个合并,以简化对一个更大集合的循环。 如果我们然后创建一个从id 到它在结果数组中的索引的小映射,我们可以更新qty 的运行总数。

    $map = [];
    $result = [];
    
    // Merge the two and do as per usual, create a mapping
    // from id to index and update the qty at the corresponding index.
    foreach (array_merge($a1, $a2) as $subarr) {
        $id = $subarr['id'];
    
        if (!key_exists($id, $map)) {
            $index = array_push($result, $subarr) - 1;
            $map[$id] = $index;
    
            continue;
        }
    
        $result[$map[$id]]['qty'] += $subarr['qty'];
    }
    
    echo '<pre>', print_r($result, true), '</pre>';
    

    输出:

    Array
    (
        [0] => Array
            (
                [id] => 1
                [qty] => 2
            )
        [1] => Array
            (
                [id] => 2
                [qty] => 1
            )
    )
    

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 1970-01-01
      • 2014-02-15
      • 2016-07-20
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      相关资源
      最近更新 更多