创建一个数组,其中键是唯一的,重复键的值将是键的总和

时间:2014-01-04 15:01:02

标签: php arrays

我创建了一个数组:

Array
(
    [0] => Array
        (
            [product_price] => 88.0000
            [category_id] => 13
        )

    [1] => Array
        (            
            [product_price] => 180.0000
            [category_id] => 13          
        )

    [2] => Array
        (
            [product_price] => 180.0000
            [category_id] => 13
        )

    [3] => Array
        (            
            [product_price] => 150.0000
            [category_id] => 13         
        )
    [4] => Array
        (            
            [product_price] => 200.0000
            [category_id] => 14            
        )        



)

上面的数组只会创建两个索引(0和1)。在这里,我想删除删除重复的数组 category_id ,并将添加所有重复值 product_price 将添加。我想要像:

Array
(
    [0] => Array
        (
            [product_price] => 598.0000
            [category_id] => 13            
        )
    [1] => Array
        (
            [product_price] => 200.0000
            [category_id] => 14            
        )

)

3 个答案:

答案 0 :(得分:0)

创建一个以category_id为键的地图。为map定义自定义插入方法,检查密钥是否已存在,然后将product_price添加到旧的

答案 1 :(得分:0)

试试这段代码:

此处$cart是您的数组名称

$res_arr = array(); 

    foreach($cart as $p)
    {
        $found = false;
        foreach($res_arr as $k => $r)
        {
            if($r["category_id"]==$p["category_id"])
            {
                $res_arr[$k]["product_price"] += $p["product_price"];
                $found = true;
            }       
        }
        if(! $found)
            $res_arr[] = $p;
    }
    print_r($res_arr);

答案 2 :(得分:0)

这是代码 -

<?php
    $arr=array(
        0 => array
            (
                'product_price' => 88.0000,
                'category_id' => 13
            ),

        1 => array
            (            
                'product_price' => 180.0000,
                'category_id' => 13          
            ),

        2 => array
            (
                'product_price' => 180.0000,
                'category_id' => 13
            ),

        3 => array
            (            
                'product_price' => 150.0000,
                'category_id' => 13         
            ),
        4 => array
            (            
                'product_price' => 200.0000,
                'category_id' => 14            
            )
    );

    //logic here
    $cat_arr=array();
    $reduced_arr=array();

    //Fetching unique category_id.
    foreach($arr as $sub_arr)
    {
        if(!in_array($sub_arr['category_id'], $cat_arr))
            $cat_arr[]=$sub_arr['category_id'];
    }

    //Getting the final result
    foreach($cat_arr as $cat)
    {
        $price=0;
        foreach($arr as $prod)
        {
            if($prod['category_id']==$cat)
                $price+=$prod['product_price'];
        }
        $reduced_arr[]= array('product_price' => $price, 'category_id' => $cat);
    }

    print_r($reduced_arr);
?>

O / P

Array ( [0] => Array ( [product_price] => 598 [category_id] => 13 ) [1] => Array ( [product_price] => 200 [category_id] => 14 ) )