php - Split Multiple Arrays Into Unique Groups -


i'm having hard time thinking of solution problem (maybe because it's monday). have multiple arrays of user members (email address) , want split them unique groups.

data example:

$members = array(     'group one' => array(         'user a',         'user b',         'user c',         'user d'     ),     'group two' => array(         'user a',         'user b',         'user d'     ),     'group three' => array(         'user a',         'user e'     ) );     

now want separate them unique groups , have results this:

array (     0 => array (         'groups' => array (             0 => 'group one'         ),         'members' => array (             0 => 'user c'         )     ),     1 => array (         'groups' => array (             0 => 'group one',             1 => 'group two'         ),         'members' => array (             0 => 'user b',             1 => 'user d'         )     ),     2 => array (         'groups' => array (             0 => 'group one',             1 => 'group two',             2 => 'group three'         ),         'members' => array (             0 => 'user a'         )     ),     3 => array (         'groups' => array (             0 => 'group three'         ),         'members' => array (             0 => 'user e'         )     ) ) 

looks doing want. getting unique combinations of groups $pool array. in getvalues function getting values exist in every group pool (with array intersect) in $a, , values other groups (with array merge) in $b. returning users present in every group of pool not in other group array_diff.

<?php  $members = array(     'group one' => array(         'user a',         'user b',         'user c',         'user d'     ),     'group two' => array(         'user a',         'user b',         'user d'     ),     'group three' => array(         'user a',         'user e'     ) );  $keys = array_keys($members); $len = count($keys);  function getvalues(&$result = array(), $members, $pool) {     $a = null;     $b = array();     foreach ($members $group => $values) {         if (in_array($group, $pool)) {             $a = (null === $a) ? $values : array_intersect($a, $values);         } else {             $b = array_merge($b, $values);         }     }      if ($ret = array_diff($a, $b)) {         $result[] = array(             'groups' => $pool,             'members' => array_values($ret),         );     } }  ($i = 0; $i < $len; ++$i) {     $pool = array($keys[$i]);     ($j = $i; $j < $len; ++$j) {         if ($j > $i) {             $pool[] = $keys[$j];         }         getvalues($result, $members, $pool);     } }  print_r($result); 

here output:

array (     [0] => array         (             [groups] => array                 (                     [0] => group 1                 )              [members] => array                 (                     [0] => user c                 )          )      [1] => array         (             [groups] => array                 (                     [0] => group 1                     [1] => group 2                 )              [members] => array                 (                     [0] => user b                     [1] => user d                 )          )      [2] => array         (             [groups] => array                 (                     [0] => group 1                     [1] => group 2                     [2] => group 3                 )              [members] => array                 (                     [0] => user                 )          )      [3] => array         (             [groups] => array                 (                     [0] => group 3                 )              [members] => array                 (                     [0] => user e                 )          )  ) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -