如何按值对php数组进行排序,并将数组添加到另一个数组

时间:2012-02-07 17:37:04

标签: php arrays steam-web-api

好的,所以我正在编写一个应用程序,允许我从任何注册的人的数据库中查看蒸汽数据。

我遇到了一个问题。首先,多用户的蒸汽API不是标准化的。 (例如,每次刷新this时,用户的位置都会发生变化(这是什么类型的API?!)

由于steam没有标准化API,我必须自己做,所以在做了json_decode($ url,true)之后。它不是一个关联数组。

我想用蒸汽ID(数字)对assoc数组进行排序,并将它们与我自己的用户数据库匹配(也包含蒸汽ID,但可以在数据库中排序),那么我该如何去做是什么?

E.g。

数组1:

array(3) {
  [0]=>
  array(2) {
    ["steam_id32"]=>
    string(17) "76561198025035234"
    ["name"]=>
    string(7) "Mitsuki"
  }
  [1]=>
  array(2) {
    ["steam_id32"]=>
    string(17) "76561197968270056"
    ["name"]=>
    string(3) "nrn"
  }
  [2]=>
  array(2) {
    ["steam_id32"]=>
    string(17) "76561197982490298"
    ["name"]=>
    string(4) "Ximp"
  }
}

数组2:

array(1) {
  ["response"]=>
  array(1) {
    ["players"]=>
    array(3) {
      [0]=>
      array(16) {
        ["steamid"]=>
        string(17) "76561197982490298"
        ["communityvisibilitystate"]=>
        int(3)
        ["profilestate"]=>
        int(1)
        ["personaname"]=>
        string(53) "……‮‮‮‮‮‮‮‮‮‮Ximp ……FUS RO DAH"
        ["lastlogoff"]=>
        int(1328569605)
        ["profileurl"]=>
        string(34) "http://steamcommunity.com/id/ximp/"
        ["avatar"]=>
        string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1.jpg"
        ["avatarmedium"]=>
        string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_medium.jpg"
        ["avatarfull"]=>
        string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_full.jpg"
        ["personastate"]=>
        int(1)
        ["realname"]=>
        string(9) "I life in"
        ["primaryclanid"]=>
        string(18) "103582791430354400"
        ["timecreated"]=>
        int(1146939839)
        ["gameextrainfo"]=>
        string(20) "The Binding Of Isaac"
        ["gameid"]=>
        string(6) "113200"
        ["loccountrycode"]=>
        string(2) "DE"
      }
      [1]=>
      array(14) {
        ["steamid"]=>
        string(17) "76561197968270056"
        ["communityvisibilitystate"]=>
        int(3)
        ["profilestate"]=>
        int(1)
        ["personaname"]=>
        string(3) "nrn"
        ["lastlogoff"]=>
        int(1328618220)
        ["profileurl"]=>
        string(34) "http://steamcommunity.com/id/nrnx/"
        ["avatar"]=>
        string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1.jpg"
        ["avatarmedium"]=>
        string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_medium.jpg"
        ["avatarfull"]=>
        string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_full.jpg"
        ["personastate"]=>
        int(1)
        ["realname"]=>
        string(9) "Nathaniel"
        ["primaryclanid"]=>
        string(18) "103582791432850562"
        ["timecreated"]=>
        int(1092771678)
        ["loccountrycode"]=>
        string(2) "US"
      }
      [2]=>
      array(14) {
        ["steamid"]=>
        string(17) "76561198025035234"
        ["communityvisibilitystate"]=>
        int(3)
        ["profilestate"]=>
        int(1)
        ["personaname"]=>
        string(23) "[ProudiA] Mitsuki Sakai"
        ["lastlogoff"]=>
        int(1328621807)
        ["commentpermission"]=>
        int(1)
        ["profileurl"]=>
        string(42) "http://steamcommunity.com/id/mitsukisakai/"
        ["avatar"]=>
        string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719.jpg"
        ["avatarmedium"]=>
        string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_medium.jpg"
        ["avatarfull"]=>
        string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_full.jpg"
        ["personastate"]=>
        int(1)
        ["realname"]=>
        string(12) "酒井å‚è¼"
        ["primaryclanid"]=>
        string(18) "103582791432752089"
        ["timecreated"]=>
        int(1273714689)
      }
    }
  }
}

1 个答案:

答案 0 :(得分:-1)

对于sortig数组,您可以找到所需的所有函数的列表here

<强>更新

首先,您必须从1d2d创建一个3d数组,您可以使用此代码创建一个简单的访问数组并进行排序(这是一个示例):

<?php
$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
   $outArr[$i] = $inArr[$i][0];
?>

然后您可以使用ksort()krsort()函数对其进行排序,并将数组添加到另一个:

<?php
  $stack = array("value1", "value2");
  array_push($stack, "value3", "value4");
  print_r($stack);
?>