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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效