some help for a beginner

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gustoid
Forum Newbie
Posts: 1
Joined: Sat Oct 09, 2010 5:35 am

some help for a beginner

Post by gustoid »

Hi,

I am attempting to generate two sets of random numbers, and have them pair up with no conflitcs.

the end result would look something like this:
1 5
2 3
5 1
4 2
3 4

I have the first set of numbers created, but I can't seem to figure out how to generate a second set and match them as shown above.

<?php
$numbers = array("1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10 ", "11 ", "12 ");
shuffle($numbers);
foreach ($numbers as $number) {
echo "$number ";
echo "<br />";
}
?>
Any help would be appreciated.

Thanks
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: some help for a beginner

Post by twinedev »

Is this what you were looking for?

Code: Select all

$intCount = count($numbers);
for($t=0;$t<$intCount;$t=$t+2) {
    echo $numbers[$t},'  ',$numbers[$t+1],"\n";
}
or

Code: Select all

$intHalf = count($numbers)/2;
for($t=0;$t<$intHalf;$t++) {
    echo $numbers[$t},'  ',$numbers[$t+$intHalf],"\n";
}
-Greg
Post Reply