我需要柜台内的柜台

I have searched for this, can't find it and it should be simple, but now I'm crosseyed. Forgive me - I've only been at this for a few months.

I need a counter within a counter in php, such that I pair $Id1 with $Id2, then $Id3, then $Id4, etc. for a single loop through, and then for the second loop pair $Id2 with $Id3, then $Id4, then $Id5 etc. I get the Ids from a sql query, then I use both of them to run a calculation of their relationship, but first I just need the structure to run these two loops, one within the other. Thanks for any help and your patience.

Edited to add what I have so far:

$collection = [];

$sql = 'SELECT id, name FROM table';                                                                                                                                                                                                                                                                                                                                                        
$result = mysql_query($sql);


while($row = mysql_fetch_assoc($result))

    {           
        $collection[] = $row;
    }   
        $ct = count($collection);
        for ($i = 0; $i < $ct; $i++)                
        {

            $Id1 = $collection[$i]['id'];       
            for ($j = 0; $j < $ct; $j++)
                {
                    $Id2 = $collection[$j]['id'];   
                    if($Id1 != $Id2){
                    echo 'IDs: ' . $Id1 . '   ' . $Id2 . '<br>';
                    }

                }

        }           

}   

If you fetch the IDs from your query into an array like this: $ids = [1,2,3,4,5,6,7,8];, you can get a list of pairs using array_chunk, then shifting off the first element, and repeating this process until the array is empty.

while ($ids) {                          // repeat until empty
    $pairs = array_chunk($ids, 2);      // split into pairs
    foreach ($pairs as $pair) {
        if (count($pair) == 2) {        // verify that you have a pair (last element will 
                                        // have only one item when count($ids) is odd)
            var_dump($pair);            // do something with your pair of IDs
        }
    }
    $remove_first = array_shift($ids);  // shift off the first ID
}

Please note that using array_shift like this will destroy your input array, so if you need to do something else with it as well, make a copy of it before using this approach.