我如何借这个json来定价呢?

时间:2016-05-03 15:44:09

标签: php jquery json

这是我目前的json数组是否可以在价格下降的情况下使用数组?我无法弄清楚我需要做些什么来获得正确的输出

Array
(
    [5952418516] => Array
        (
            [id] => 5952418516
            [name] => AK-47 | Redline (Field-Tested)
            [image] => https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5lpKKqPrxN7LEmyVQ7MEpiLuSrYmnjQO3-UdsZGHyd4_Bd1RvNQ7T_FDrw-_ng5Pu75iY1zI97bhLsvQz
            [type] => weapon
            [price] => 4.73
        )

    [5912178664] => Array
        (
            [id] => 5912178664
            [name] => AWP | Worm God (Field-Tested)
            [image] => https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAZx7PLfYQJW-9W4kb-GkvP9JrafxG0GscMhjLqW9t7zjVfn8hc6MmmnI9CTclRrYgrU_Vfowefs18K6uIOJlyXpUGetdA
            [type] => weapon
            [price] => 0.78
        )
[5746271006] => Array
    (
        [id] => 5746271006
        [name] => AWP | Hyper Beast (Factory New)
        [image] => https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJK9cyzhr-JkvbnJ4Tdn2xZ_Ismju2To9qm31Hsr0ZsMTryJo_BcANrMwyCrFLrx7vrhJa1vZrByXo2pGB8sr2_Epwm
        [type] => weapon
        [price] => 54.2
    )

)

问候

3 个答案:

答案 0 :(得分:1)

是的,通过使用uasort,您可以比较值并根据比较对数组进行排序。

uasort($array, function($a,$b) {
   return $a['price'] < $b['price'] ? -1 : 1;
});

https://eval.in/564645

答案 1 :(得分:0)

出于此目的使用uasort功能:

// $arr is your initial array
uasort($arr, function($a, $b){
    return $b['price'] - $a['price']; // sorting by descending price
});

答案 2 :(得分:0)

你在这里遇到哪种语言? PHP还是Javascript? 提供的大多数解决方案都是基于PHP的。如果您可以控制源代码,并希望在将数据作为JSON发送之前对数据进行排序; PHP就像@RomanPerekhrest和@hdˑ已经建议的那样 - 虽然我只是为了这样强调而再次添加到这里:

    <?php
        $gamePayload = "COLLECTION_OR_ARRAY_OF_DATA_FROM_DATASOURCE_LIKE_DB_OR_SO";
        uasort($gamePayload, function($gp1, $gp2){
            return $gp2['price'] - $gp1['price']; 
        });
        // SORTS THE THE DATA IN DESCENDING ORDER.
        // IF YOU WANT IT SORTED IN ASCENDING ORDER;
        // CHANGE::  return $gp2['price'] - $gp1['price'] 
        // TO::      return $gp1['price'] - $gp2['price']

另一方面,如果JSON数据来自另一个源(这意味着您无法从数据源更改订单),Javascript就是这样的:

<script type="text/javascript">

var jsonData    = [
    {
        id:     5952418516,
        name:   'AK-47 | Redline (Field-Tested)',
        image:  'https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09-5lpKKqPrxN7LEmyVQ7MEpiLuSrYmnjQO3-UdsZGHyd4_Bd1RvNQ7T_FDrw-_ng5Pu75iY1zI97bhLsvQz',
        type:   'weapon',
        price:  4.73
    },

    {
        id:     5912178664,
        name:   'AWP | Worm God (Field-Tested)',
        image:  'https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAZx7PLfYQJW-9W4kb-GkvP9JrafxG0GscMhjLqW9t7zjVfn8hc6MmmnI9CTclRrYgrU_Vfowefs18K6uIOJlyXpUGetdA',
        type:   'weapon',
        price:  0.78
    },

    {
        id:     5746271006,
        name:   'AWP | Hyper Beast (Factory New)',
        image:  'https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot621FAR17PLfYQJK9cyzhr-JkvbnJ4Tdn2xZ_Ismju2To9qm31Hsr0ZsMTryJo_BcANrMwyCrFLrx7vrhJa1vZrByXo2pGB8sr2_Epwm',
        type:   'weapon',
        price:  54.2
    }

];

var mm = jsonData.sort(function(jsonObj1, jsonObj2) {
    return parseFloat(jsonObj1.price) - parseFloat(jsonObj2.price);
});
// DISPLAYS THE DATA IN DESCENDING ORDER.
// IF YOU WANT IT SORTED IN ASCENDING ORDER;
// CHANGE::  return parseFloat(jsonObj1.price) - parseFloat(jsonObj2.price) 
// TO::      return parseFloat(jsonObj2.price) - parseFloat(jsonObj1.price)
console.log(mm);