Unique values in multidimensional array

Discussion in 'PHP' started by PoPSiCLe, May 21, 2014.

  1. #1
    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.
     
    PoPSiCLe, May 21, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    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.
     
    sarahk, May 21, 2014 IP
    PoPSiCLe likes this.
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    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)?
     
    ThePHPMaster, May 21, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    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 :)
     
    PoPSiCLe, May 22, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    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.
     
    deathshadow, May 22, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    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 :)
     
    PoPSiCLe, May 23, 2014 IP