【问题标题】:Create variables with different names in a foreach loop在 foreach 循环中创建具有不同名称的变量
【发布时间】:2014-07-01 17:05:39
【问题描述】:
$id = '76561198036414106';
$key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$link2 = file_get_contents('http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=' . $key . '&steamid=' . $id . '&relationship=friend');
    $myarray2 = json_decode($link2, true);

    foreach ($myarray2['friendslist']['friends'] as $type) {
           echo "<br>id: " . $type['steamid'];
           [save this $type['steamid'] into a var, then do a foreach again and save down the next to another variable]
        }

如何将 foreach 输出的输出保存到多个变量? var1、var2、var3 等,直到数组不再输出任何内容。

我想要做的是这个脚本会在 Steam 上获取我所有朋友的社区 ID,然后我将获取这些 ID 并通过另一个 api 调用运行它们,这将获取我的朋友们玩某个游戏的次数在过去 2 周内。所以我需要的只是如何保存 api 输出的所有社区 ID,并将它们全部放在一个单独的变量中。

【问题讨论】:

    标签: php api steam


    【解决方案1】:

    将这些 SteamID 保存到一个数组中。

    $steamids = array();
    foreach ( ... )
    {
        array_push($steamids, $type['steamid']);
    }
    

    此时您可以使用另一个foreach 来调用steamids

    foreach($steamids as $steamid)
    {
        // Call your API
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 2021-12-24
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多