【问题标题】:How to sort php array by value and add an array to another如何按值对php数组进行排序并将数组添加到另一个数组
【发布时间】:2012-02-07 17:37:03
【问题描述】:

好的,所以我正在编写一个应用程序,让我可以从注册用户的数据库中查看 Steam 数据。

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

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

我想按steam ID(数字)对assoc数组进行排序,并将它们与我自己的用户数据库(也包含steam ID,但可以在数据库中排序)进行匹配,那么我该怎么做那个?

例如

数组 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)
      }
    }
  }
}

【问题讨论】:

  • "(这是什么 API 做的?!)" -- 你得到的是一个对象,而不是一个数组。 JSON 中的对象本身并不是有序的。
  • 啊,我明白了,但至少不应该按照我调用 API 的顺序吗?
  • 不清楚您想要实现什么:如果您只想将条目与数据库中的数据进行比较/匹配 - 使用 ID 进行 - 为什么顺序很重要?如果它是出于显示目的(然后顺序确实很重要) - 您可以在执行信息与数据库的匹配/比较时将 ID 保存到数据结构中 - 然后从数据库中选择所有相关条目(使用保存的 ID)并使用“order by”。

标签: php arrays steam-web-api


【解决方案1】:

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

更新:

首先您必须从2d3d 创建一个1d 数组,您可以使用此代码创建一个易于访问且可排序的数组(这是一个示例):

<?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);
?>

【讨论】:

  • 他想通过元素中的数组中的键对数组进行排序。这并不能解决他的问题。
  • @thenetimp 首先阅读php.net/manual/en/array.sorting.php,然后给出-1。请多加注意
  • @thenetimp 例如:http://www.php.net/manual/en/function.krsort.phphttp://www.php.net/manual/en/function.ksort.php
  • 嗨,bizzare,我通读了array.sorting,但结果头疼,您能指出我应该使用哪种排序吗?谢谢!
  • @Omfgitsasalmon krsort() 或 ksort()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 1970-01-01
相关资源
最近更新 更多