Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个JSON数组中的一对多关系,PHP中的查找结果_Php_Json - Fatal编程技术网

两个JSON数组中的一对多关系,PHP中的查找结果

两个JSON数组中的一对多关系,PHP中的查找结果,php,json,Php,Json,我有两个单独的JSON数组。数组1称为匹配,数组2称为参与者。参与者可以参加许多比赛。我需要一些关于如何从matches数组中的matches中查找玩家id并从participants数组返回相应的参与者名称的指导 比赛包含:每场比赛的球员1号和球员2号。 参与者包含每个玩家的id。 player1_id和player2_id将等于参与者数组中的一个id,除非player1_id或player2_id为null 样本匹配数组: [ { "match": {

我有两个单独的JSON数组。数组1称为匹配,数组2称为参与者。参与者可以参加许多比赛。我需要一些关于如何从matches数组中的matches中查找玩家id并从participants数组返回相应的参与者名称的指导

比赛包含:每场比赛的球员1号和球员2号。 参与者包含每个玩家的id。 player1_id和player2_id将等于参与者数组中的一个id,除非player1_id或player2_id为null

样本匹配数组:

[
  {
    "match": {
      "attachment_count": null,
      "created_at": "2015-01-19T16:57:17-05:00",
      "group_id": null,
      "has_attachment": false,
      "id": 23575258,
      "identifier": "A",
      "location": null,
      "loser_id": null,
      "player1_id": 16543993,
      "player1_is_prereq_match_loser": false,
      "player1_prereq_match_id": null,
      "player1_votes": null,
      "player2_id": 16543997,
      "player2_is_prereq_match_loser": false,
      "player2_prereq_match_id": null,
      "player2_votes": null,
      "round": 1,
      "scheduled_time": null,
      "started_at": "2015-01-19T16:57:17-05:00",
      "state": "open",
      "tournament_id": 1086875,
      "underway_at": null,
      "updated_at": "2015-01-19T16:57:17-05:00",
      "winner_id": null,
      "prerequisite_match_ids_csv": "",
      "scores_csv": ""
    }
  },
  {
    "match": {
      "attachment_count": null,
      "created_at": "2015-01-19T16:57:17-05:00",
      "group_id": null,
      "has_attachment": false,
      "id": 23575259,
      "identifier": "B",
      "location": null,
      "loser_id": null,
      "player1_id": 16543994,
      "player1_is_prereq_match_loser": false,
      "player1_prereq_match_id": null,
      "player1_votes": null,
      "player2_id": 16543996,
      "player2_is_prereq_match_loser": false,
      "player2_prereq_match_id": null,
      "player2_votes": null,
      "round": 1,
      "scheduled_time": null,
      "started_at": "2015-01-19T16:57:17-05:00",
      "state": "open",
      "tournament_id": 1086875,
      "underway_at": null,
      "updated_at": "2015-01-19T16:57:17-05:00",
      "winner_id": null,
      "prerequisite_match_ids_csv": "",
      "scores_csv": ""
    }
  },
  {
    "match": {
      "attachment_count": null,
      "created_at": "2015-01-19T16:57:17-05:00",
      "group_id": null,
      "has_attachment": false,
      "id": 23575260,
      "identifier": "C",
      "location": null,
      "loser_id": null,
      "player1_id": null,
      "player1_is_prereq_match_loser": false,
      "player1_prereq_match_id": 23575258,
      "player1_votes": null,
      "player2_id": null,
      "player2_is_prereq_match_loser": false,
      "player2_prereq_match_id": 23575259,
      "player2_votes": null,
      "round": 2,
      "scheduled_time": null,
      "started_at": null,
      "state": "pending",
      "tournament_id": 1086875,
      "underway_at": null,
      "updated_at": "2015-01-19T16:57:17-05:00",
      "winner_id": null,
      "prerequisite_match_ids_csv": "23575258,23575259",
      "scores_csv": ""
    }
  }
]
我的示例参与者数组:

[
  {
    "participant": {
      "active": true,
      "checked_in_at": null,
      "created_at": "2015-01-19T16:54:40-05:00",
      "final_rank": null,
      "group_id": null,
      "icon": null,
      "id": 16543993,
      "invitation_id": null,
      "invite_email": null,
      "misc": null,
      "name": "Participant #1",
      "on_waiting_list": false,
      "seed": 1,
      "tournament_id": 1086875,
      "updated_at": "2015-01-19T16:54:40-05:00",
      "challonge_username": null,
      "challonge_email_address_verified": null,
      "removable": true,
      "participatable_or_invitation_attached": false,
      "confirm_remove": true,
      "invitation_pending": false,
      "display_name_with_invitation_email_address": "Participant #1",
      "email_hash": null,
      "username": null,
      "attached_participatable_portrait_url": null,
      "can_check_in": false,
      "checked_in": false,
      "reactivatable": false
    }
  },
  {
    "participant": {
      "active": true,
      "checked_in_at": null,
      "created_at": "2015-01-19T16:54:43-05:00",
      "final_rank": null,
      "group_id": null,
      "icon": null,
      "id": 16543994,
      "invitation_id": null,
      "invite_email": null,
      "misc": null,
      "name": "Participant #2",
      "on_waiting_list": false,
      "seed": 2,
      "tournament_id": 1086875,
      "updated_at": "2015-01-19T16:54:43-05:00",
      "challonge_username": null,
      "challonge_email_address_verified": null,
      "removable": true,
      "participatable_or_invitation_attached": false,
      "confirm_remove": true,
      "invitation_pending": false,
      "display_name_with_invitation_email_address": "Participant #2",
      "email_hash": null,
      "username": null,
      "attached_participatable_portrait_url": null,
      "can_check_in": false,
      "checked_in": false,
      "reactivatable": false
    }
  },
  {
    "participant": {
      "active": true,
      "checked_in_at": null,
      "created_at": "2015-01-19T16:57:10-05:00",
      "final_rank": null,
      "group_id": null,
      "icon": null,
      "id": 16543996,
      "invitation_id": null,
      "invite_email": null,
      "misc": null,
      "name": "Participant #3",
      "on_waiting_list": false,
      "seed": 3,
      "tournament_id": 1086875,
      "updated_at": "2015-01-19T16:57:10-05:00",
      "challonge_username": null,
      "challonge_email_address_verified": null,
      "removable": true,
      "participatable_or_invitation_attached": false,
      "confirm_remove": true,
      "invitation_pending": false,
      "display_name_with_invitation_email_address": "Participant #3",
      "email_hash": null,
      "username": null,
      "attached_participatable_portrait_url": null,
      "can_check_in": false,
      "checked_in": false,
      "reactivatable": false
    }
  },
  {
    "participant": {
      "active": true,
      "checked_in_at": null,
      "created_at": "2015-01-19T16:57:12-05:00",
      "final_rank": null,
      "group_id": null,
      "icon": null,
      "id": 16543997,
      "invitation_id": null,
      "invite_email": null,
      "misc": null,
      "name": "Participant #4",
      "on_waiting_list": false,
      "seed": 4,
      "tournament_id": 1086875,
      "updated_at": "2015-01-19T16:57:12-05:00",
      "challonge_username": null,
      "challonge_email_address_verified": null,
      "removable": true,
      "participatable_or_invitation_attached": false,
      "confirm_remove": true,
      "invitation_pending": false,
      "display_name_with_invitation_email_address": "Participant #4",
      "email_hash": null,
      "username": null,
      "attached_participatable_portrait_url": null,
      "can_check_in": false,
      "checked_in": false,
      "reactivatable": false
    }
  }
]
我期望的结果是:

  • 参与者4对参与者2
  • 参与者1对参与者3
对于匹配数组中的每个匹配,依此类推

等等


据我所知,由于一对多的关系,合并两个数组将不起作用,但我可能在合并方面没有受过教育。任何关于从何处开始的指导都将通知。我也研究过过滤,但这似乎也不是我的答案。

首先通过id对参与者进行索引,它存储整个数组,以备以后需要任何其他详细信息。最终结果是一个名为
$participants
的数组,该数组由id索引

然后它循环遍历匹配项并提取两个id,如果它们都有值,则从存储的数组中输出名称,否则使用
Unknown
作为名称

$participant = json_decode(file_get_contents("t.json"), true);
// Extract subarrays
$participants = array_column($participant, "participant");
// index by id
$participants = array_column($participants, null, "id");

$matches = json_decode(file_get_contents("a.json"), true);
foreach ( $matches as $match )  {
    // Extract player ID's
    $id1 = $match['match']['player1_id'];
    $id2 = $match['match']['player2_id'];

    if ( $id1 && $id2 ) {
        echo $participants[$id1]["name"] . " vs " . $participants[$id2]["name"].PHP_EOL;
    }
    else    {
        echo "Unknown vs Unknown".PHP_EOL;
    }
}

工作起来很有魅力!非常感谢!