I have an array with a random amount of elements (it's a online game * random amount of users each time). I want to split this array into two teams, how is this achievable?
Example of an Array i have where 4 & 3 are ID's of users.
Array ( [0] => 4 [1] => 3 )
I haven't found so much information on this, and the information i have found is pretty hard for me to understand, so i would deeply appreciate some sort of explanation on how i can do this.
If i have not provided enough information, just let me know! Thank you for taking your time reading my question!
//UPDATE There will be 2 teams. There is no max number of players in each team, but preferably around 15 players per team.
You could use array chunk http://php.net/manual/en/function.array-chunk.php
$input_array = array('a', 'b', 'c', 'd', 'e');
$count = ceil(count($input_array)/2); // since you require 2 teams, you need to divide by 2
shuffle($input_array);
$teams =array_chunk($input_array,$count);
this will create two teams.
You could use array_chunk and additionally add the shuffle function over the array
$array = [1, 2, 3, 4];
$teams = 2;
$result = array_chunk(shuffle($array), sizeof($array) / $teams)