Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What we are going to do, is split the players in 2 groups: contenders 1 &amp; contenders 2. We're going to fill contenders a with consecutive players of the same school:</p> <p>If we name the schools a single letter for brevity, what we'll essentially have is:</p> <pre><code> A A A A B B B C C C D D E E F F </code></pre> <p>Or, if we flip it, it becomes clearer:</p> <pre><code> A C A C A D A D B E B E B F C F </code></pre> <p>What happens if one school has > the half of the total number of players? Well, let's see:</p> <pre><code> A A A A A A B B C D </code></pre> <p>So:</p> <pre><code> A A &lt;= one A vs. A, which is unavoidable, but the method still works. A B A B A C A D </code></pre> <p>I cheated a bit here: I sorted out the players from the biggest school first. It however still works as long as we group schools together. Let's take my output of <code>$a = range('A','F'); shuffle($a):</code>, which here resulted in: FCADBE, which gives us:</p> <pre><code> F A F D C D C B C B A B A E A E </code></pre> <p>.. which works, also for A > half:</p> <pre><code> C A A A &lt;= that one double again, unavoidable A D A B A B </code></pre> <p>Let's break this into parts. What we have to do is:</p> <ol> <li>sort the players by school</li> <li>break this sorted array into 2 groups.</li> <li>create pairs by adding on from the first group &amp; one from the second group, in order.</li> </ol> <p>The advantage of cutting it down is so you can find answers:</p> <ol> <li><a href="https://stackoverflow.com/search?q=%5Bphp%5D+php+sort+multidimensional+array">hundreds of questions on SO</a></li> <li><a href="https://stackoverflow.com/questions/5393028/how-can-i-take-an-array-divide-it-by-two-and-create-two-lists">this is a nice answer</a></li> <li>We could use a <a href="http://www.php.net/manual/en/class.multipleiterator.php" rel="nofollow noreferrer">MultiIterator</a>, which would be a logical (and working) choice, but I'll show you another short way of creating pairs of 2 arrays (or triplets if you have 3 arrays, etc.)</li> </ol> <p>So, let's do that:</p> <pre><code> //sort by school $players = ... your array ... usort($players,function($playerA, $playerB){ return strcmp($playerA['school'], $playerB['school']); }); //voila, sorted //break this into 2 groups: $size = ceil(count($players) / 2); // round UP $groupA = array_slice($players,0,$size); $groupB = array_slice($players,$size); //create our duels array: $duels = array_map(null, $groupA, $groupB); </code></pre> <p><code>$duels</code> now has this content with your input:</p> <pre><code>[ [ { "name": "juan", "school": "ABC" }, { "name": "arnold", "school": "DEF" } ], [ { "name": "leo", "school": "ABC" }, { "name": "ash", "school": "ECD" } ], [ { "name": "simon", "school": "DEF" }, { "name": "luke", "school": "ECD" } ] ] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload