1

I came across a kind of a puzzle today during my coding. I wanted to share and get help from you.

I make a query in to the MySql Database and get results as in an array. So I have an output like this:

 $rows=array(n) { // for each n value elements can have different values
    ["val1"] => string(3) "abc"
    ["val2"] => string(3) "def"
    ["val3"] => string(3) "ghi"
    ["val4"] => string(3) "jkl"
}

So let's say n=4 for instance and i need to create unique pairs that same numbers of pair do not occur in the queue.
What I need is as in the example:

n= 1, 2, 3, 4

Pairs I need to get:

1-2, 1-3, 1-4, 2-3, 2-4, 3-4

I need to avoid the pairs like 2-1. 3-1, 4-1, 3-2, 4-2, 4-3 and 1-1, 2-2, 3-3, 4-4.
For Every Pair, I will Check if this pair of arrays are equal.
How can I do that?

9
  • do you want to end up with... abc-def, abc-ghi, abc-jkl, def-ghi, def-jkl, ghi-jkl..? Commented Nov 15, 2013 at 1:17
  • nope, i just need to get the unique pairs to check if these two arrays have to same values, so i need to get tem paired up once so it does not check it two times. Commented Nov 15, 2013 at 1:22
  • so........ you literally want, given n=4, just this? 1-2, 1-3, 1-4, 2-3, 2-4, 3-4 Commented Nov 15, 2013 at 1:23
  • n can be any number, so i need to create a logic that does this pars without duplicate regardless what is the value of 'n'. Commented Nov 15, 2013 at 1:25
  • right, n can be whatever, but if n is 4, you want just a list of those pairs of numbers? (I'm confused at what you want) Commented Nov 15, 2013 at 1:26

3 Answers 3

1
<?php

$n = 4;

for($i = 1; $i<=$n; $i++)
    for($x = 1; $x<=$n; $x++)
        if($i != $x && !isset($array[$x][$i]))
            $array[$i][$x] = '';


//echo '<pre>';
//var_dump($array);
//echo '</pre>';
?>

to output use

<?php

for($i = 1; $i<=$n; $i++)
    for($x = 1; $x<=$n; $x++)
        if(isset($array[$i][$x]))
            echo $i.'-'.$x.', ';

?>

which would be:

if $n = 4

1-2, 1-3, 1-4, 2-3, 2-4, 3-4,

if $n = 10

1-2, 1-3, 1-4, 1-5, 1-6, 1-7, 1-8, 1-9, 1-10, 2-3, 2-4, 2-5, 2-6, 2-7, 2-8, 2-9, 2-10, 3-4, 3-5, 3-6, 3-7, 3-8, 3-9, 3-10, 4-5, 4-6, 4-7, 4-8, 4-9, 4-10, 5-6, 5-7, 5-8, 5-9, 5-10, 6-7, 6-8, 6-9, 6-10, 7-8, 7-9, 7-10, 8-9, 8-10, 9-10,

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. That works with no problem. i get all the necessary pairs with this and no duplicates. if the value of n is 100 Do you think it would take too long?
@Justin: 1. what if you try? 2. what if you calculate ?
@Justin it shouldn't take long with a 100, I just did it instantaneously... just use the updated answer, as I don't set the array to = 'YES', I only set it to ''
@zerkms i did not understand what you meant, can you explain what you mean? thank you.
@Arian i am positive on your answer.
1

I'm using the array and I just remove the elements once all the pairs have been made, probably there's a better solution using only for's

  $n = 5;
$array = array("abc","def","ghi","jkl","mno");
$r_temp = $array;
$r_result = array();
foreach($array as $r){
    $i = 0;
    while($i < $n-1){
        array_push($r_result,$r_temp[0].$r_temp[$i+1]);
        $i++;
    }
    $n--;
    array_shift($r_temp); //Remove the first element since all the pairs are used
}

print_r($r_result);

Output would be

Array ( [0] => abcdef [1] => abcghi [2] => abcjkl [3] => abcmno [4] => defghi [5] => defjkl [6] => defmno [7] => ghijkl [8] => ghimno [9] => jklmno )

1 Comment

this is not what i am trying to get. my target is to make pairs of the element identifying numbers.
0

It is an old question, however it looks like a good example of an XY Problem.

First, a direct answer to your question:

$n = 4;
$pairs = [];
for ($i = 1; $i <= $n; ++$i) {
    for ($j = $i+1; $j <= $n; ++$j) {
        $pairs[] = [$i, $j];
    }
}

(You can check it here).

However, if I understand correctly what you were looking for, it is easier to do so directly with and SQL query:

SELECT mycolumn, COUNT(*) AS total FROM mytable GROUP BY mycolumn HAVING total > 1;

This query will return all repeated entries of column mytable.mycolumn and number of appearances.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.