I have this code:
$um = array("PHP", "JAVA", "MySQL")
$a = count($um)
for ($i = 0; $i < $a; $i++) {
for ($x = $i + 1; $x <$a; $x++) {
$arr1 [] = array($um[$i],$um[$x]);
}
}
This will output something like these combinations:
PHP[0] JAVA[1]
PHP[0] MySQL[2]
JAVA[1] MySQL[2]
Well, works without any problem.
But now, i want to change the output to something like:
PHP[0] JAVA[1]
JAVA[1] MySQL[2]
MySQL[2] PHP[0]
The logic will be the same for the three array elements or even 10, and so on.
Any idea about this ?
Just use modular arithmetic: you want to pair indices (i,i+1), where the
i+1wraps around to 0 if it becomes too large.So for
$a=3this displays according to indices:Modulus Operator docs