2017-07-25 181 views
0

請問有人請告訴我怎樣才能排序這種JSON數組"name"按字母順序升序如何在PHP中對多維JSON數組進行排序?

{ 
    "response": { 
     "game_count": 86, 
     "games": [ 
      "10": { 
       "appid": 10, 
       "name": "Counter-Strike", 
       "playtime_forever": 7604, 
       "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5", 
       "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f", 
       "has_community_visible_stats": true 
      }, 
      "80": { 
       "appid": 80, 
       "name": "Counter-Strike: Condition Zero", 
       "playtime_forever": 62, 
       "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236", 
       "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f" 
      }, 
       . 
       . 
       . 
      ] 
    } 
} 
+0

我將使用'lodash'和'_.sortBy'函數。 – Hackerman

+0

@Hackerman lodash在PHP文件中? – BeetleJuice

+0

Oooh,你想用PHP排序....所以你應該使用'json_decode'並開始按名稱排列遊戲數組......我會讓你我的客戶端解決方案,以防萬一https:///jsfiddle.net/d18s7ndk/ – Hackerman

回答

1

你的問題並沒有說清楚你的代碼在哪種語言。我假設PHP是因爲PHP標籤問題。

我的做法($json是生JSON):

// decode JSON into an array 
$arr = json_decode($json,true); 

// usort allows sorting by custom attribute 
usort(
    $arr['response']['games'], 
    function($a,$b){ return $a['name'] < $b['name'] ? -1 : 1;} 
    ); 

// print and check order 
print_r($arr); 

// turn it back into JSON 
$json = json_encode($arr); 

Live demo

文檔:usort

1

你以這種方式得到要求的結果。

var json_array = { 

    "response": { 
     "game_count": 86, 
     "games": [ 
      { 
       "appid": 10, 
       "name": "Counter-Strike", 
       "playtime_forever": 7604, 
       "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5", 
       "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f", 
       "has_community_visible_stats": true 
      }, 
      { 
       "appid": 80, 
       "name": "Counter-Strike: Condition Zero", 
       "playtime_forever": 62, 
       "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236", 
       "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f" 
      }, 
      . 
      . 
      . 
     ] 

    } 
} 

json_array.response.games.sort(sort_by('name', false, function(a){ 
    return a.toUpperCase() 
})); 

var sort_by = function(a, m, n){ 

var key = n ? 
    function(p) {return n(p[a])} : 
    function(q) {return q[a]}; 

m = !m ? 1 : -1; 

return function (x, y) { 
    return x = key(x), y = key(y), m * ((x > y) - (y > x)); 
} 
} 
1

你可以做這樣的事情:

<?php 

    $json = '{ 
     "response": { 
      "game_count": 86, 
      "games": [ 
       { 
        "appid": 10, 
        "name": "Counter-Strike", 
        "playtime_forever": 7604, 
        "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5", 
        "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f", 
        "has_community_visible_stats": true 
       }, 
       { 
        "appid": 80, 
        "name": "Counter-Strike: Condition Zero", 
        "playtime_forever": 62, 
        "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236", 
        "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f" 
       }, 
       { 
        "appid": 23, 
        "name": "Art Attack", 
        "playtime_forever": 76604, 
        "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5", 
        "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f", 
        "has_community_visible_stats": true 
       } 
      ] 
     } 
}'; 

$arrayConvertedJson = json_decode($json, true); 
$games = $arrayConvertedJson['response']['games']; 
uasort($games, 'sortArray'); 

function sortArray($a, $b){ 
    if($a['name'] < $b['name']){ 
     return -1; 
    } 
    if($a['name'] > $b['name']){ 
     return 1; 
    } 
    return 0; 
} 
$arrayConvertedJson['response']['games'] = $games; 

?> 
相關問題