Question : PHP remove duplicate values from multidimensional array

I'm trying to figure out how to merge duplicate array entries, in a multidimensional array.

Here's an excerpt of my data:

1:
2:
3:
4:
5:
6:
Accounting/Business - 86.02
   1->Demonstrate analytical and critical-thinking skills with direct application to business environments. => 85.71
   2->Communicate ideas and information effectively both orally and in writing. => 90.48
   3->Demonstrate skill in the use of computer software application and technology in business and industry. => 90.48
   4->Demonstrate skill in the use of computer software application and technology in business and industry. => 85.71


What I'm trying to do is find anywhere that the text is the same, and merge them into a single key=>value pair.  But aside from just merging the text, the numbers need to be added (and averaged) together to create a new number.

Ideally the above snip would become the following snip:

1:
2:
3:
4:
5:
Accounting/Business - 86.02
   1->Demonstrate analytical and critical-thinking skills with direct application to business environments. => 85.71
   2->Communicate ideas and information effectively both orally and in writing. => 90.48
   3->Demonstrate skill in the use of computer software application and technology in business and industry. => 88.095


The new number (88.095) created by the following = ((90.48+85.71)/2).

Anyone have any ideas?  I have been trying to get this all day, but I can't seem to find any info that helps or figure it out myself...  Thanks for any assistance in this...

Answer : PHP remove duplicate values from multidimensional array

I think I understand you problem, give this ago :)


<?php

// Input array
$array = array(
      array('a' => 10),
      array('b' => 20),
      array('c' => 20),
      array('c' => 15)
);

// Create a new array for storage
$newArray = array();

// Loop through each item
foreach($array as $item) {
      // Find the array keys of the item (['a']...['c'], etc)
      $keys = array_keys($item);
      
      // If the keys count is what we are expecting
      if (count($keys) == 1) {
            
            // Check if this value has already been processed
            if (array_key_exists($keys[0], $newArray)) {
                  
                  // If so then increase the count and add the value
                  $newArray[$keys[0]]['count']++;
                  $newArray[$keys[0]]['value'] += $item[$keys[0]];
                  
            } else {
                  
                  // Other wise add this item to the new array
                  $newArray[$keys[0]] = array(
                        'count' => 1,
                        'value' => $item[$keys[0]]
                  );
            }
      }
}

// Average out the values and format into the required output
$outputArray = array();
foreach($newArray as $key => &$value) {
      $outputArray[] = array($key => ($value['value'] /= $value['count']));
}


// Output array
var_dump($finalArray);
Random Solutions  
 
programming4us programming4us