当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Create an array where key is unique and duplicate key's value will be sum of keys

问题描述:

I have created an array:

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

)

)

The above array will be create only tow index(0 and 1). Here I want to remove remove duplicate arrays category_id wise and will add all duplicate value's product_price will be add.I want to like as :

Array

(

[0] => Array

(

[product_price] => 598.0000

[category_id] => 13

)

[1] => Array

(

[product_price] => 200.0000

[category_id] => 14

)

)

网友答案:

try this code:

Here $cart is your array name

$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);
网友答案:

Create a map with category_id as key. Define custom insert method for map which checks that if key already exists then add product_price to old one

网友答案:

Here is the code-

<?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 ) )
分享给朋友:
您可能感兴趣的文章:
随机阅读: