2

I want to sum array in same key and different value.

Array
(
    0 => Array
        (
            'locker_id' => 3,
            'locker_model' => 1,
            'qty' => 2
        ),

1 => Array
    (
        'locker_id' => 3,
        'locker_model' => 1,
        'qty' => 1
    ),

2 => Array
    (
        'locker_id' => 2,
        'locker_model' => 1,
        'qty' => 2
    ),

 3 => Array
    (
        'locker_id' => 3,
        'locker_model' => 1,
        'qty' => 2
    ),
4 => Array
    (
        'locker_id' => 2,
        'locker_model' => 1,
        'qty' => 2
    )

);

I want Output it

Array
(
    0 => Array
        (
            'locker_id' => 3,
            'locker_model' => 1,
            'qty' => 5
        ),
1 => Array
    (
        'locker_id' => 2,
        'locker_model' => 1,
        'qty' => 4
    )
);

Thanks.

2
  • 1
    From your description it is hard to tell what you are trying to do. Can you be a bit more specific? Commented Jan 30, 2014 at 11:19
  • I think "locker_id" should be "2" in the 2nd key of 2nd array Commented Jan 30, 2014 at 11:21

6 Answers 6

2

You could browse your array while creating a new one with keys like "locker_id-locker_model", then you can easily check if your locker exists in this array with array_key_exists function.

$inputArray = array( ... ); // this is your message array
$outputArray = array();

foreach( $inputArray as $locker )
{
    if( !array_key_exists( $locker["locker_id"]."-".$locker["locker_model"], $outputArray ) )
    {
        $outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ] = array(
            "locker_id" => $locker["locker_id"],
            "locker_model" => $locker["locker_model"],
            "qty" => 0
        );
    }

    $outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ]["qty"]++;
}

var_dump( $outputArray );
Sign up to request clarification or add additional context in comments.

Comments

1
    $dataArr = array
    (
        0 => array
            (
                'locker_id' => 3,
                'locker_model' => 1,
                'qty' => 2
            ),

        1 => array
            (
                'locker_id' => 3,
                'locker_model' => 1,
                'qty' => 1
            ),

        2 => array
            (
                'locker_id' => 2,
                'locker_model' => 1,
                'qty' => 2
            ),

         3 => array
            (
                'locker_id' => 3,
                'locker_model' => 1,
                'qty' => 2
            ),
        4 => array
            (
                'locker_id' => 2,
                'locker_model' => 1,
                'qty' => 2
            )

    );

    $sumArr = array();
    if(count($dataArr)>0){
        foreach($dataArr as $data){

            if(!isset($sumArr[$data['locker_id']])){
                $sumArr[$data['locker_id']] = $data;
            }
            $sumArr[$data['locker_id']]['qty'] +=$data['qty'];
        }
    }
  echo "<pre>";print_r($sumArr);

Comments

1

Considering $array1 is your first array,

$array2 = array();
foreach($array1 as $k=>$v) {
    if(!isset($array2[$v['locker_id']])) {
        $array2[$v['locker_id']] = $v;
    } else {
        $array2[$v['locker_id']]['qty'] += $v['qty'];
    }
}

print_r($array2);

Comments

1

Try this $arr is your array

$j = 0;
$new = array();
$new[0][qty];

for($i=0;$i<arraylength;$i++){
foreach($arr as $key => $value) {
if($arr[$i][lockerid] == $arr[$key][lockerid]) {
$new[$j][lockerid] = $arr[$key][lockerid];
$new[$j][lockermodel] = $arr[$key][lockermodel];
$new[$j][qty] = $new[$i][qty] + $arr[$key][qty];
J++;
}
}
}

2 Comments

What does $new[0][qty]; line mean?
remove that it will work without that also.
1
$result = array();  

foreach($array as $key => $value){
  if(isset($value['locker_id'])){
     $result[$value['locker_id']]['qty'] += $value['qty'];
  }
  else{
     $result[$value['locker_id']] = $value;
  }

}

You have to loop over your array and save it to another let's say $result. In $result you should put the locker_id as key and then you only have to verify if that key exists. If it is you add the qty value, if isn't you have to add the entire new item.

1 Comment

Your answer would be much more useful if it explained how it works and how it answers the question. That way the asker learns more than if they just copy your answer.
0

try this

$ARR_OUTPUT = array();

    // $arr will contains your input array
    foreach($arr as $arr_val)
    {
       $locker_id = $arr_val['locker_id'];
       $locker_model = $arr_val['locker_id'];
       $qty = $arr_val['locker_id'];



       if(isset($ARR_OUTPUT[$locker_id][$locker_model]))
       {    
            $ARR_OUTPUT[$locker_id][$locker_model] += $qty;
       }
       else
       {
            $ARR_OUTPUT[$locker_id][$locker_model] = $qty;
       }



        }

    echo "<pre>";
    print_r($ARR_OUTPUT);

$ARR_OUTPUT2 = array();

    foreach($ARR_OUTPUT as $locker_id=>$arr_values)
    {
        foreach($arr_values as $locker_model=>$qty)
        {
            $arr_temp['locker_id'] = $locker_id;
            $arr_temp['locker_model'] = $locker_model;
            $arr_temp['qty'] = $qty;
            $ARR_OUTPUT2[] = $arr_temp;
        }   
    }

    echo "<pre>";
    print_r($ARR_OUTPUT2);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.