PHP remove duplicate values from multidimensional array

cbastian-hill
cbastian-hill used Ask the Experts™
on
I'm trying to figure out how to merge duplicate array entries, in a multidimensional array.

Here's an excerpt of my data:

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

Open in new window


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:

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

Open in new window


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...

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
What is the "multi-dimensional" part of this question?  Could you please print the array out with var_dump() and post that in the code snippet?  I think you want to find matching keys and average the values, but I am not sure of the array structure so I can't show you the code yet.

thanks, ~Ray
Most Valuable Expert 2011
Top Expert 2016

Commented:
Afterthought... Please use echo "<pre>"; before the var_dump() - it makes the output much more readable.  And please present a fairly large excerpt (perhaps all the data) so we do not overlook the outliers in the test cases.

Best, ~Ray
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);

Author

Commented:
Thanks...  I wasn't really thinking...  Following your tip with a slight change, I got it to work...  Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial