php - Finding duplicate values in multidimensional array -


i'm trying code system find user accounts using same ips, , print out matches keys. have coded part data pulled mysql , nicely put in multidimensional associative array, input looks (the key userid, values ip addresses):

$users = array(                100 => array("1.1.1.1","2.2.2.2","3.3.3.3"),                200 => array("1.1.1.1","4.4.4.4","5.5.5.5"),                300 => array("1.1.1.1","4.4.4.4","7.7.7.7")               ); 

the expected output be:

array (     [1.1.1.1] => array         (             [0] => 100             [1] => 200             [2] => 300         )      [4.4.4.4] => array         (             [0] => 200             [1] => 300         ) ) 

i have searched , used trial , error multiple nested foreach loop through arrays, tried array_intersect find duplicates, nothing gets me close expected output. think i'm overthinking problem , solution easy, cannot seem close expected output. appreciated, thank you.

$output = array(); // loop through each user foreach ($users $id => $ips) {     // loop through each ip address     foreach ($ips $ip) {         // add ip address, if not present         if (!isset($output[$ip])) {             $output[$ip] = array();                  }         // add user id ip address' array         $output[$ip][] = $id;     } } 

Comments