dongxuan8227 2018-01-09 11:19
浏览 277
已采纳

为数组值组生成唯一ID

I have an asosiative array which contains data about teams and players.

Example:

$arr = array(
  array('teamID'=> '','teamName' => 'USA', 'playerName'='John'),
  array('teamID'=> '','teamName' => 'USA', 'playerName'='Peter'),
  array('teamID'=> '12','teamName' => 'Norway', 'playerName'='Zigmund'),
  array('teamID'=> '','teamName' => 'USA', 'playerName'='Parker'),
  array('teamID'=> '','teamName' => 'Norway', 'playerName'='Jan'),
  array('teamID'=> '','teamName' => 'USA', 'playerName'='Hector'),
  array('teamID'=> '','teamName' => 'Germany', 'playerName'='Alexander'),
  array('teamID'=> '','teamName' => 'Slovakia', 'playerName'='Ivan')
);

I want to generate unique ID for each team if it is not present, if the id is present for some team use it on same team names if they dont exist there, and do not use id's which already exists.

What I have did is simple check if not exists ad index of the foreach loop, bet then it is per player not per team.

Expected outcome:

$arr = array(
  array('teamID'=> '1','teamName' => 'USA', 'playerName='John'),
  array('teamID'=> '1','teamName' => 'USA', 'playerName'='Peter'),
  array('teamID'=> '12','teamName' => 'Norway', 'playerName'='Zigmund'),
  array('teamID'=> '1','teamName' => 'USA', 'playerName'='Parker'),
  array('teamID'=> '12','teamName' => 'Norway', 'playerName'='Jan'),
  array('teamID'=> '1','teamName' => 'USA', 'playerName'='Hector'),
  array('teamID'=> '2','teamName' => 'Germany', 'playerName'='Alexander'),
  array('teamID'=> '3','teamName' => 'Slovakia', 'playerName'='Ivan')    
);

Any ideas on how to solve this?

  • 写回答

5条回答 默认 最新

  • dongmaqiu6084 2018-01-09 11:29
    关注

    This would solve your problem (as one of many possible solutions). Here we have an array holding each team name as a key, and an incremented numerical ID for every occurence of a new team name. Then we check if the key exists, if it does, we reuse the ID that is assigned to it. If it doesn't exist, we create it and add an ID, and then increment the integer.

    $teams_with_ids = [];
    $teamids = [];
    $i=0;
    foreach( $arr AS $team ){
        if( array_key_exists($team['teamName'], $teamids) ){
            $team['teamID'] = $teamids[$team['teamName']];
        } else {
            $teamids[$team['teamName']] = $i;
            $team['teamID'] = $i;
            $i++;
        }
        array_push($teams_with_ids, $team);
    }
    

    EDIT:

    As pointed out in the comment, the above solution did not account for existing ID's on some teams. This does:

    $teams_with_ids = [];
    $teamids = [];
    $existing_ids = array_filter((array_map(function($team){ if( !empty( $team['teamID'] ) ) return intval($team['teamID']); },$arr)));
    $i=0;
    foreach( $arr AS $team ){   
        if( array_key_exists($team['teamName'], $teamids) ){
            $team['teamID'] = $teamids[$team['teamName']];
        } else {
            if( in_array( $i, $existing_ids ) ) $i++; // Adding +1 to $i since the ID is already taken
            $teamids[$team['teamName']] = (!empty($team['teamID']) && in_array($team['teamID'], $existing_ids)) ? $team['teamID'] : $i;
            $team['teamID'] = (empty($team['teamID'])) ? $i : $team['teamID'];
            if( empty($team['teamID'] ) ) $i++;
        }
        array_push($teams_with_ids, $team);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥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之后自动重连失效