2012-02-07 96 views
0

好的,所以我正在編寫一個應用程序,允許我從任何註冊的數據庫中查看蒸汽數據。如何按值排序php數組並向其中添加數組

我遇到了問題。首先,針對多個用戶的蒸汽API不是標準化的。 (例如,每次刷新this,用戶的位置發生變化(什麼樣的API的做到這一點?)

由於蒸汽不規範的API,我得做我自己,所以做了json_decode後($ url,true)。它不是一個assoc數組。

我想通過蒸汽ID(這是數字)對assoc數組進行排序,並將它們與我自己的用戶數據庫(也包含蒸汽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) 
     } 
    } 
    } 
} 
+0

「(做什麼樣的API的這個?!)」 - - 你找回一個對象,而不是一個數組。 JSON中的對象並非固有地排序。 – Interrobang 2012-02-07 17:42:18

+0

嗯,我明白了,但是不應該按照我稱之爲API的順序嗎? – Omfgitsasalmon 2012-02-07 17:49:32

+0

目前還不清楚你想實現什麼:如果你只是想比較/匹配條目與數據庫中的數據 - 使用ID來做 - 爲什麼順序很重要? 如果是爲了顯示目的(然後訂單確實很重要) - 您可以將ID保存到數據結構中,同時預先執行信息與數據庫的匹配/比較 - 然後選擇數據庫中的所有相關條目保存的ID)並使用「order by」。 – alfasin 2012-02-07 17:51:19

回答

-1

對於sortig數組,你可以找到你需要here

更新所有功能的列表:

首先你必須創建一個1d陣列從2d3d您可以使用此代碼來使一個簡單的訪問數組和排序(這樣的例子):

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

那麼你就可以ksort()krsort() function.and排序它添加一個陣列到另一個:

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

他想要通過元素中的數組中的鍵對數組進行排序。這並不能解決他的問題。 – thenetimp 2012-02-07 17:43:37

+0

@thenetimp首先閱讀http://php.net/manual/en/array.sorting.php然後給予-1.pay更多的關注請 – bizzr3 2012-02-07 17:45:36

+0

@thenetimp例如:'http://www.php.net/ manual/en/function.krsort.php'或'http://www.php.net/manual/en/function.ksort.php' – bizzr3 2012-02-07 17:49:23