如何在双数组JSON(PHP)上显示信息

时间:2018-12-28 13:26:27

标签: php arrays json double

我想知道如何显示以下信息。

链接(JSON):

https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents

我希望拥有的东西

"teams": [
      [
        {
          "provider": "psn",
          "username": "Germania1992"
        },
        {
          "provider": "psn",
          "username": "killzoneprofi"
        },
        {
          "provider": "psn",
          "username": "ayozetf87"
        },
        {
          "provider": "psn",
          "username": "Seith911"
        },
        {
          "provider": "psn",
          "username": "domibreu92"
        }
      ],
      [
        {
          "provider": "psn",
          "username": "Thejuankarboy"
        },
        {
          "provider": "psn",
          "username": "Gamermad101"
        },
        {
          "provider": "psn",
          "username": "Izdrap"
        },
        {
          "provider": "psn",
          "username": "Guerra_sv"
        },
        {
          "provider": "psn",
          "username": "TriX_FollOoW_YT"
        }
      ]
    ],

我想展示不同球队的刻痕

  

例如:小组1 = Germania1992,killzoneprofi,ayozetf87,Seith911,   domibreu92

谢谢

2 个答案:

答案 0 :(得分:1)

您必须肯定要进行两次foreach。

<?php
// Get the json of the team
$team = team("https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents");

// Display the teams info
foreach($team as $nb=>$data){
    echo "Team $nb<br />";
    foreach($data as $key=>$value){
        echo "Provider: ".$value->provider."<br />";
        echo "Username: ".$value->username."<br />";
    }
    echo "<hr>";
}

// Returns the team array of the json
function team($jsonURL){
  $content=file_get_contents($jsonURL);
  $data=json_decode($content);
  return $data->data->teams;
}

?>

您可以将团队编号增加1,以避免第一个为0

上面将显示以下屏幕截图(您可以随意设置输出格式)

enter image description here

答案 1 :(得分:-1)

    $result = [];
    $counter = 0;
    dump($array = json_decode(file_get_contents('https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents')));
    dump($t = array_column((array)$array, 'teams'));
    foreach ($t as $r) {
        foreach ($r as $p) {
            $counter++;
            foreach ($p as $value){
                $result["team$counter"][] = $value->username;
            }
        }
    }
    var_dump($result);