doubo4824 2018-05-04 15:14
浏览 75
已采纳

将php数组拆分成团队

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.

  • 写回答

2条回答 默认 最新

  • doujue1246 2018-05-04 15:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?