I have an array as follows (example): Array ( [mon_1_0_02/06/2014] => Array ( [0] => 2 [1] => 3 ) [wed_1_1_04/06/2014] => Array ( [0] => 28 ) [fri_1_0_06/06/2014] => Array ( [0] => 32 ) [tue_1_0_10/06/2014] => Array ( [0] => 2 ) [tue_1_0_17/06/2014] => Array ( [0] => 2 ) ) PHP: What I would like to be able to is make sure that the values within each inner array is only processed once - while I can do that by adding each value to another array and then do a check to see if the value already exist in the array, I'm pondering if there is a simpler way? Note, I do NOT want to get rid of the KEYS - just duplicate values, regardless of keys.
so in this case monday and the 2 tuesdays are duplicates because they have a value of 2, right? the brute force way is just to keep looping through the array before you add a new value to it. slow but probably not perceptibly slow.
What is your definition of duplicates? Was it like what Sarah mentioned or just duplicate inside each array independently (which I am not seeing in the values posted)?
Yeah, duplicates as Sarah mentioned. I'm currently "brute-forcing" it by just iterating through all the arrays, adding the values to a new array, and then array_unique that one, and then looping through it. It works fine, I was just wondering if there was a better way
I would probably refactor the array in reverse, so as to be able to preserve the dates and time associations... because I would think that information would be important. <?php $test = [ 'mon_1_0_02/06/2014' => [ 2, 3 ], 'wed_1_1_04/06/2014' => [ 28 ], 'fri_1_0_06/06/2014' => [ 32 ], 'tue_1_0_10/06/2014' => [ 2 ], 'tue_1_0_17/06/2014' => [ 2 ] ]; function processUnique($data) { $result = []; foreach ($data as $key => $values) { foreach ($values as $val) $result[$val][] = $key; } return $result; } echo '<pre>',print_r(processUnique($test)), '</pre>'; ?> Code (markup): Should give you: Array ( [2] => Array ( [0] => mon_1_0_02/06/2014 [1] => tue_1_0_10/06/2014 [2] => tue_1_0_17/06/2014 ) [3] => Array ( [0] => mon_1_0_02/06/2014 ) [28] => Array ( [0] => wed_1_1_04/06/2014 ) [32] => Array ( [0] => fri_1_0_06/06/2014 ) ) 1 Code (markup): Yeah, it's effectively brute-forcing, but it's also data preserving. Using the value as the new key also means you don't have to waste time checking for the value, so it could be faster too.
That's actually quite clever - not so important in this particular case (since in this case it's just that I need to get the unique values for getting the corresponding information pr. user, to send out information emails) - hence the dates/keys aren't that important in this setup. However, the reverse array trick is probably gonna do another part a lot easier than I first thought. Thanks