0

Ok the thing is, Ive read and tried a lot of solutions found here based on complete duplicateness, BUT I need something a little bit different and cant figure it out...

Because I only want to get and remove the duplicate value if only 1 of the array values is duplicate. And also later combine the two but that's not that difficult.

$a = array(
    array(
        'id' => 1,
        'qty' => 1
    ),
    array(
        'id' => 0,
        'qty' => 1
    ),
    array(
        'id' => 1,
        'qty' => 2
    )
);

What I want the outcome to be:

$b = array(
    array(
        'id' => 1,
        'qty' => 3
    ),
    array(
        'id' => 0,
        'qty' => 1
    )
);
3
  • Please, ask specific questions instead of asking for someone to do the work for you. What have you tried? mattgemmell.com/what-have-you-tried Commented Nov 26, 2014 at 21:19
  • why not merge the arrays with key values and make a new array with it? That way you wont have duplicates.. is there a reason you want to remove the duplicates? Only the quantity changes from what I'm understanding. Commented Nov 26, 2014 at 21:20
  • @PedroMoreira - Im sorry Im not showing what I've tried. Like I said I tried a lot of multidimensional array sorting answers here but they all focused on being a full duplicate, so id ánd qty being equal. Since im not that familiar with Multidimensional arrays I thought I could ask for help.. alda1234 - A lot changes but this is just a small example of how the situation could be. I thought about merging but again couldn't solve it myself Commented Nov 26, 2014 at 23:06

3 Answers 3

3

You can do something like the following:

<?php
$a = array(
    array(
        'id' => 1,
        'qty' => 1
    ),
    array(
        'id' => 0,
        'qty' => 1
    ),
    array(
        'id' => 1,
        'qty' => 2
    )
);

$idsList = array();
$res = array();
foreach ($a as $arr){
  if (!in_array($arr['id'], $idsList)){
    $res[$arr['id']] = $arr;
    $idsList[] = $arr['id'];
  }
  else{
      $res[$arr['id']]['qty'] += $arr['qty'];

  }
}

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

To get array like that:

Array
(
    [1] => Array
        (
            [id] => 1
            [qty] => 3
        )

    [0] => Array
        (
            [id] => 0
            [qty] => 1
        )

)

Checkout This DEMO: http://codepad.org/UP0G7WnE

Sign up to request clarification or add additional context in comments.

1 Comment

choose yours because it wasn't dependent on the other values in the array, thanks for your quick/great answer!
3
$a = array(
array(
    'id' => 1,
    'qty' => 1
),
array(
    'id' => 0,
    'qty' => 1
),
array(
    'id' => 1,
    'qty' => 2
)
);

$b = array();
$ids = array();
for($i = 0; $i < count($a); $i++)
{ 
  if(in_array($a[$i]['id'], $ids))
  {    
    $j = array_search($a[$i]['id'], array_values($ids));    
    $b[$j]['qty'] += $a[$i]['qty'];
  }
 else
 {
   $b[$i]['id'] = $a[$i]['id'];
   $b[$i]['qty'] = $a[$i]['qty'];
   $ids[] = $a[$i]['id'];
 }
}

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

Comments

2

This could be a solution:

$temp = array();

foreach ($a as $data){

    if (isset($temp[$data['id']])){
        $temp[$data['id']] += $data['qty'];             
    } else {
        $temp[$data['id']] = $data['qty'];
    }

}

// Format array

$b = array();

foreach ($temp as $id => $qty){

    $b[] = array(
        'id' => $id,
        'qty' => $qty
    );  

}

Output will be:

Array
(
    [0] => Array
        (
            [id] => 1
            [qty] => 3
        )

    [1] => Array
        (
            [id] => 0
            [qty] => 1
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.