【问题标题】:How to Remove duplicate values from Multidimentional array with single unique value in all array如何从多维数组中删除所有数组中具有单个唯一值的重复值
【发布时间】:2016-07-26 17:38:10
【问题描述】:

我有像下面这样的多维数组,

$product = array( 
            "2e471a22b1b994a7cb3f3a40cee9fba2" => array (
               "product" => 6004,
               "unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab,    
               "product_id" => 51,
        "line_total"=>99,
         "quantity"=>1,
        "data"=> array(
        "id"=> 51,
        "post"=>array(
            "ID"=>51,
            "post_title"=>"Prodcut four - control",            
            ),
        "price"=>99    
        )
            ),
    "a7d0f813832ec8a2bf24269ff7145d0c" => array (
               "product" => 6004,
               "unique_key" => c30d1ca26d30aa3dc3c9aa04f0b585ce,    
               "product_id" => 51,
        "line_total"=>99,
        "quantity"=>1,
        "data"=> array(
        "id"=> 51,
        "post"=>array(
            "ID"=>51,
            "post_title"=>"Prodcut four - control",            
            ),
        "price"=>99    
        )
            )
         );

需要根据 'product_id' 数组值删除重复值,并根据产品数量增加数量值。 注意:上述数组也有“唯一键”,因此数组结果中需要任何单个唯一键。

Expected Result should be:
$resultproduct = array( 
        "2e471a22b1b994a7cb3f3a40cee9fba2" => array (
           "product" => 6004,
           "unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab,    
           "product_id" => 51,
    "line_total"=>99,
     "quantity"=>2,
    "data"=> array(
    "id"=> 51,
    "post"=>array(
        "ID"=>51,
        "post_title"=>"Prodcut four - control",            
        ),
    "price"=>99    
    )
        ));

【问题讨论】:

  • 在 foreach 中循环产品数组,然后按照您喜欢的方式构建数组结构,如果您找到相同的产品 ID,则递增计数器
  • 嘿 Ramkumar,数据来自哪里?
  • @MangeshSatheIND 也需要删除重复值,所有产品都有唯一键,所以 array_unique 不起作用。
  • @SyedMohamedAladeen 在上面的数组中,所有产品都有唯一的键,所以 array_unique 不起作用。您给出的链接在数组索引中的所有值都是相同的,但它在我的数组中不起作用,因为所有数组索引都有不同的唯一键。

标签: php arrays multidimensional-array array-unique


【解决方案1】:

Working code at eval.in

我尝试使代码易于理解,因此变量和代码行数比绝对需要的要多。

解释:

1) 需要使用原始product 数组索引之一作为输出表键,例如产品 51 为“2e471a22b1b994a7cb3f3a40cee9fba2”。

2) 需要将输入 productId 快速关联到输出键。所以,我使用了一个查找表 ProductIdList 匹配 productIdoutput 键。

然后是一个两阶段查找,以在输出中找到条目并向其添加数量。

代码:

// create a product_id => first key table
$productIdList = array();

// output...
$productTotal = array();

foreach ($product as $origIndex => $entry) {

    $curProductId = $entry['product_id'];

    // check product_id exists in the lookup...
    if (isset($productIdList[$curProductId])) { // add to the total...

        $productTotalIndex = $productIdList[$curProductId];

        $productTotal[$productTotalIndex]['quantity'] +=  $entry['quantity'];
    }
    else { // add the entry to the output and the productIdList...

        $productIdList[$curProductId] = $origIndex;

        $productTotal[$origIndex] = $entry;
    }
}

输出:总计数组:

Array
(
    [2e471a22b1b994a7cb3f3a40cee9fba2] => Array
        (
            [product] => 6004
            [unique_key] => 3a8a5cb029ee3b92cfc90de23e2329ab
            [product_id] => 51
            [line_total] => 99
            [quantity] => 2
            [data] => Array
                (
                    [id] => 51
                    [post] => Array
                        (
                            [ID] => 51
                            [post_title] => Prodcut four - control
                        )
                    [price] => 99
                )
        )

    [test02] => Array
        (
            [product] => 6664
            [unique_key] => c30d1ca26d30aa3dc3c9aa04f0b585ce
            [product_id] => 666
            [line_total] => 99
            [quantity] => 579
            [data] => Array
                (
                    [id] => 666
                    [post] => Array
                        (
                            [ID] => 666
                            [post_title] => Prodcut 666 - control
                        )
                    [price] => 99
                )
        )
)

productId 到原始密钥列表:

array (size=2)
  51 => string '2e471a22b1b994a7cb3f3a40cee9fba2' (length=32)
  666 => string 'test02' (length=6)

【讨论】:

    【解决方案2】:

    您需要遍历每个产品并使用 product_id 作为新数组的键。这会增加数量,因此应该适用于 1 以外的任何数量。

    $result = [];
    
    foreach ($product as $p)
    {
        if (isset($result[$p['product_id']]))
        {
            $result[$p['product_id']]['quantity']+= $p['quantity'];
        }
    
        else
        {
            $result[$p['product_id']] = $p;
        }
    }
    
    print_r($result);
    

    【讨论】:

      猜你喜欢
      • 2022-12-22
      • 2016-08-05
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      相关资源
      最近更新 更多