如何从Multidimentional数组中删除重复值,并在所有数组中使用单个唯一值

时间:2016-04-06 10:57:42

标签: php arrays multidimensional-array array-unique

我有如下所示的多维数组,

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

2 个答案:

答案 0 :(得分:0)

您需要遍历每个产品并使用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);

答案 1 :(得分:0)

Working code at eval.in

我尝试使代码易于理解,因此更多的变量和代码行是绝对必需的。

说明:

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

2)需要快速将输入productId与输出键相关联。因此,我使用了一个匹配ProductIdListproductId密钥的查找表output

然后进行两阶段查找以在输出中查找条目并向其添加数量。

代码:

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