0

I've done some searching all i could find is this unfortunately thats not exactly what i'm looking for.

My problem is, i have a list of user ids in an array that could vary in size from 0 to 30+ users.

looks somehting like:

$arr = array(1,2,3);

What i need is to be able to find all pairs possible with this users:

(1 - 2)
(1 - 3)

(2 - 1)
(2 - 3)

(3 - 1)
(3 - 2)

the idea is to be able to create contact lists on a messaging platform so each one has each other on their contact list.

the examples on the question linked above gives me repeating elements and i can't have repeating elements also don't need 3 elements pared together only 2.

Community
  • 1
  • 1
dan k
  • 139
  • 2
  • 11
  • 1
    2 loops, should be easy, tried anything? –  Jul 15 '15 at 02:54
  • http://stackoverflow.com/questions/20438065/find-all-possible-2-letter-combinations-in-a-given-string-using-php/20438207#20438207 – chiliNUT Jul 15 '15 at 02:57

1 Answers1

1

You have two objectives from the way I see it:

  1. Display all possible pairs between a and b numbers
  2. Don't display the same combination twice

You can achieve this with two loops and by keeping track of what you've processed already:

$arr = range(1, 30);
$alreadyProcessed = array();

foreach ($arr as $first) {
    // Loop the array twice (as @Dagon mentioned)
    foreach ($arr as $second) {
        // Keep track of what you've already processed
        $combination = array($first, $second);
        // Sorting the numbers will ensure that 2 - 1 and 1 - 2 are the same
        sort($combination);
        // Ensure they aren't the same number and you haven't already processed them
        if ($first === $second || in_array($combination, $alreadyProcessed)) {
            continue;
        }
        // Output as per your example
        echo "($first - $second)" . PHP_EOL;
        // Add it to the list of what you've already processed
        $alreadyProcessed[] = $combination;
    }
}

NOTE: this kind of logic is bad. You're performing an exponential number of repetitions because you're looping an array inside a loop of the same array. This particular example will perform 30 to-the-power-of 30 repetitions (a big, big number of cycles). That's only with 30 entries. What happens if your user base grows to 10,000? Bye bye server.

scrowler
  • 24,273
  • 9
  • 60
  • 92
  • 1
    @Dagon For people who live in 2015 in other words :) – Rizier123 Jul 15 '15 at 03:13
  • Updated for the oldies – scrowler Jul 15 '15 at 03:14
  • @scrowler Can't we just leave the "oldies" behind, please :)?! – Rizier123 Jul 15 '15 at 03:14
  • i know you would not be surprised at just how many servers are not running 5.4.0+ yet (work server number 1 =PHP Version => 5.3.3-7+squeeze26 ) –  Jul 15 '15 at 03:22
  • we just got our last 5.2 server up to 5.4.5 the other day. It was glorious – chiliNUT Jul 15 '15 at 03:32
  • @scrowler you have a point there, this is not really practical when numbers are too big... do you have a better solution for this? my initial ideal was to pair users that signup on the past 90 days. – dan k Jul 15 '15 at 03:40
  • If you want to pair all users by default then why not make it work the opposite way, as in "blacklist" users if a record/match exists? – scrowler Jul 15 '15 at 03:59