3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $competitors = [1, 2, 3, 4, 5 , 6, 7, 8]; $rounds = log( count( $competitors ), 2 ) + 1; // round one for( $i = 0; $i < log( count( $competitors ), 2 ); $i++ ) { $seeded = array( ); foreach( $competitors as $competitor ) { $splice = pow( 2, $i ); $seeded = array_merge( $seeded, array_splice( $competitors, 0, $splice ) ); $seeded = array_merge( $seeded, array_splice( $competitors, -$splice ) ); } $competitors = $seeded; } $events = array_chunk( $seeded, 2 ); if( $rounds > 2 ) { $round_index = count( $events ); // second round for( $i = 0; $i < count( $competitors ) / 2; $i++ ) { array_push( $events, array( array( 'from_event_index' => $i, 'from_event_rank' => 1 ), // rank 1 = winner array( 'from_event_index' => ++$i, 'from_event_rank' => 1 ) ) ); } $round_matchups = array( ); for( $i = 0; $i < count( $competitors ) / 2; $i++ ) { array_push( $round_matchups, array( array( 'from_event_index' => $i, 'from_event_rank' => 2 ), // rank 2 = loser array( 'from_event_index' => ++$i, 'from_event_rank' => 2 ) ) ); } $events = array_merge( $events, $round_matchups ); for( $i = 0; $i < count( $round_matchups ); $i++ ) { array_push( $events, array( array( 'from_event_index' => $i + count( $competitors ) / 2, 'from_event_rank' => 2 ), array( 'from_event_index' => $i + count( $competitors ) / 2 + count( $competitors ) / 2 / 2, 'from_event_rank' => 1 ) ) ); } } if( $rounds > 3 ) { // subsequent rounds for( $i = 0; $i < $rounds - 3; $i++ ) { $round_events = pow( 2, $rounds - 3 - $i ); $total_events = count( $events ); for( $j = 0; $j < $round_events; $j++ ) { array_push( $events, array( array( 'from_event_index' => $j + $round_index, 'from_event_rank' => 1 ), array( 'from_event_index' => ++$j + $round_index, 'from_event_rank' => 1 ) ) ); } for( $j = 0; $j < $round_events; $j++ ) { array_push( $events, array( array( 'from_event_index' => $j + $round_index + $round_events * 2, 'from_event_rank' => 1 ), array( 'from_event_index' => ++$j + $round_index + $round_events * 2, 'from_event_rank' => 1 ) ) ); } for( $j = 0; $j < $round_events / 2; $j++ ) { array_push( $events, array( array( 'from_event_index' => $j + $total_events, 'from_event_rank' => 2 ), array( 'from_event_index' => $j + $total_events + $round_events / 2, 'from_event_rank' => 1 ) ) ); } $round_index = $total_events; } } if( $rounds > 1 ) { // finals array_push( $events, array( array( 'from_event_index' => count( $events ) - 3, 'from_event_rank' => 1 ), array( 'from_event_index' => count( $events ) - 1, 'from_event_rank' => 1 ) ) ); }
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.2.6