如何按关键价格从低到高或从高到低的价格对JSON对象进行排序?

时间:2017-10-13 07:54:17

标签: php json

我正在创建一个网站,其中数据存储在文本文件中,我使用带有api-s的ajax来访问此文本文件并使用它执行多个任务。我被困在如何用PHP对这个产品进行排序?这是我的products.txt JSON对象数组。

[
    {
        "id": "59d278cae7017",
        "name": "A",
        "price": "1",
        "quantity": 3,
        "image": "img_webshop\/productimage-59d74304917c2.jpg"
    },
    {
        "id": "59d27e20c8028",
        "name": "B",
        "price": "2",
        "quantity": 3,
        "image": "img_webshop\/productimage-59d743233c0cf.jpg"
    },
    {
        "id": "59d6a7ae16d15",
        "name": "C",
        "price": "3",
        "quantity": 2,
        "image": "img_webshop\/productimage-59d743392fbb5.jpg"
    },
    {
        "id": "59d6d6ee5f752",
        "name": "D",
        "price": "4",
        "quantity": 4,
        "image": "img_webshop\/productimage-59d74352d5b94.jpg"
    },
    {
        "id": "59d743d207bd5",
        "name": "E",
        "price": "5",
        "quantity": 1,
        "image": "img_webshop\/productimage-59d743d1e6e64.jpg"
    },
    {
        "id": "59d74451225ac",
        "name": "F",
        "price": "6",
        "quantity": 1,
        "image": "img_webshop\/productimage-59d7445120871.jpg"
    }
]

显然这里是php的开头:

//GETTING FROM FILE:

$sajProducts = file_get_contents( 'products.txt' );
$ajProducts = json_decode( $sajProducts );

?>

对于fornt-end我只得到了html:

 <!-- PRODUCTS display and editing is handled here according USER or ADMIN-->

    <div id="pageViewProducts" class="page">
            <div class="lblWrapper"> 
                  <button type="button" class="btnShowPage" id="btnCreateProduct" data-showThisPage="pageCreateProduct"><i class="fa fa-plus"></i>&nbsp;Add Product</button>
                  <form id="frmSortBy">
                    <p>Sort by:</p>
                    <select>
                      <option id="oPriceLowToHigh">PRICE (LOW TO HIGH)</option>
                      <option id="oPriceHighToLow">PRICE (HIGH TO LOW</option>
                      <option id="oOnSale" selected>ON SALE</option>
                    </select>
                  </form>
                  <div id="lblProductList">
                    <!-- Generated dynamically -->
                  </div>
            </div>
    </div>

首先,我感兴趣的是如何在api中处理这个问题,前端的javascript可以稍后出现。

0 个答案:

没有答案