Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a multi-dimensional array of values posted from a form and I am trying to not just de-dupe them, but sum the values from the duplicated arrays so that these can be posted as one, larger value.

The array posted is as follows:

Array
(
[0] => Array
    (
        [0] => 4
        [1] => 27/11/2013
    )

[1] => Array
    (
        [0] => 4
        [1] => 27/11/2013
    )

[2] => Array
    (
        [0] => 2
        [1] => 
    )

[3] => Array
    (
        [0] => 2
        [1] => 
    )

)

For each sub-array, value 0 is a quantity and value 1 is a date (which is optional). The final entry in the array is a qty of 2 with a blank date which was added automatically because the required total was 12.

I am then looking through the sub-arrays using a foreach loop (below) to check for duplicate dates.

foreach ($qty_date_array as $key => $value)
{
    if(in_array($value[1], $exists) && !in_array($value[1], $duplicates))
    {
        $duplicates[] = $value[1]; 
    }
    else 
    {
        $exists[] = $value[1]; 
    }

The output of $duplicates is...

Array
(
    [0] => 27/11/2013
    [1] => 
)

I guess I then want to look at the values reached in the array and foreach of them, find the associated original qty value(s) and sum them, but I'm not sure how to proceed further.

Array_search will only return the key, so I guess that is out (and I cannot write the Qty as the key because it may be a duplicate value). Other array functions i've dabbled with, such as array_unique, collapse the array based on the indexes, so lose data.

The final multidimensional array would ideally look something like this:

Array
(
[0] => Array
    (
        [0] => 8
        [1] => 27/11/2013
    )

[2] => Array
    (
        [0] => 4
        [1] => 
    )
)

If someone can suggest a more efficient way to achieve this, I'm open to all suggestions!

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

What is the value if the “optional” date value is missing – just NULL or an empty string?

If so, something like this will work: First sum up the data in a temporary array, using the date as key; and then afterwards just transfer it to an array in the desired format:

$temp_data = $new_data = array();
foreach($data as $val) { // $data would be your original array here
  if(isset($temp_data[$val[1]])) {   // if entry for date already exists,
    $temp_data[$val[1]] += $val[0];  // add value to existing sum for that date
  }
  else {
    $temp_data[$val[1]] = $val[0];   // otherwise put it in as new value for date
  }
}
foreach($temp_data as $key => $val) {  // create numerically indexed array,
  $new_data[] = array($val, $key);     // using key (date) as item 0
}                                      // and summed up value as item 1
share|improve this answer
    
Yes, that does exactly what I want. I've tested it with about 4 different permutations and it does what I needed. :) –  Optimaximal Nov 26 '13 at 13:29
add comment
$tmp = array();
foreach($data as $key => $value){
    if(isset($tmp[$value[1]])){
        $tmp[$value[1]][0] += $value[0];        
    }else{
        $tmp[$value[1]] = $value;
    }
 } 

 $data = array_values($tmp);
share|improve this answer
add comment

You can use grouping keys directly and so avoid searching each time:

$array = [
   [4, '27/11/2013'],
   [2, ''],
   [5, ''],
   [1, '27/11/2013'],
   [3, '29/11/2013']
];
$result = [];
foreach($array as $item)
{
   //used serialize() cause value can be anything 
   //while key should be a string or int:
   $key = serialize($item[1]);
   $result[$key][0] = isset($result[$key][0])?
                         $result[$key][0]+$item[0]:
                         $item[0];
   $result[$key][1] = $item[1];
}
$result = array_values($result);

-that used short array syntax, so, for PHP<=5.3, replace array definition from [] to array()

share|improve this answer
    
That works, but it's generating error notices: Notice: Undefined index: s:10:"27/11/2013"; Notice: Undefined offset: 0 Would that be just as a result of using php 5.2.17? –  Optimaximal Nov 26 '13 at 13:17
    
@Optimaximal yes, it will. To do that fully correct, you need to add element's initialization (I've updated) –  Alma Do Nov 26 '13 at 13:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.