【问题标题】:Convert JSON nested data into PHP array of arrays将 JSON 嵌套数据转换为 PHP 数组数组
【发布时间】:2018-05-15 20:15:15
【问题描述】:

我正在从服务器实时接收 json 数组。 这是结构:

{
"success":true,
"message":"",
"result":
    [{  "MarketName":"BTC-1ST",
        "High":0.00003944,
        "Low":0.00003350,
        "Volume":1844905.50329604,
        "Last":0.00003655,
        "BaseVolume":67.77017463,
        "TimeStamp":"2017-12-01T15:49:50.037",
        "Bid":0.00003655,
        "Ask":0.00003662,
        "OpenBuyOrders":110,
        "OpenSellOrders":3412,
        "PrevDay":0.00003659,
        "Created":"2017-06-06T01:22:35.727"},

    {   "MarketName":"BTC-2GIVE",
        "High":0.00000071,
        "Low":0.00000064,
        "Volume":26833879.82630229,
        "Last":0.00000066,
        "BaseVolume":18.39542245,
        "TimeStamp":"2017-12-01T15:43:01.267",
        "Bid":0.00000065,
        "Ask":0.00000066,
        "OpenBuyOrders":136,
        "OpenSellOrders":1663,
        "PrevDay":0.00000063,
        "Created":"2016-05-16T06:44:15.287”}]
}

我需要做的是将输入的 json 中的一些值分配给一个 php 数组数组,该数组必须具有以下矩阵结构:

| Symbol_first_array_element Ask_Price_first_array_element Volumes_first_array_element |
| Symbol_second_array_element Ask_Price_second_array_element Volumes_second_array_element |
| Symbol_n_array_element Ask_Price_n_array_element Volumes_n_array_element |

例如:

| 1ST 0.00003662 1844905.50329604 |
| 2GIVE 0.00000066 26833879.82630229 |

我还想仅从 MarketName 中包含“BTC”的元素填充这个数组数组。这意味着我需要在一个循环中检查我插入到数组数组中的所有对象的 MarketName 是否为“BTC-whatever else”。

你能帮帮我吗?我对php很陌生。

【问题讨论】:

  • 在 PHP 手册中查找 json_decode()
  • 一旦您尝试,我们会在您遇到困难时为您提供帮助。 1. json_decode 服务器结果 2.loop
  • 你的代码在哪里?尝试@imox 所说的方法
  • 好的,我会尽力的。

标签: php arrays json matrix multidimensional-array


【解决方案1】:

好的,问题解决了。正如有人在上面的 cmets 中所说的那样,我只需要对代码进行一些操作即可。 无论如何,这是我的解决方案:

    $json_url = 'https://bittrex.com/api/v1.1/public/getmarketsummaries';
    $jsondata = file_get_contents($json_url);
    $obj = json_decode($jsondata, TRUE); 
    $coins = array();
    $i = 0;
    $n = max(array_map('count', $obj)); 
    /*
    * With the SLOC above I get from Bittrex all the JSON data required,
    * then I fetch it, I assign it to an object, I declare the $coins array
    * and finally I assign to the $n variable the number of occurences inside 
    * the array.
    */


        for($i; $i < $n+1; $i++){

            /* Inside this if selection I check that the i-th coin is traded
            *  against BTC (Bitcoin) 
            */
            if (strpos($obj['result'][$i]['MarketName'], 'BTC-') !== false) { 


                /* The next three SLOC are self-explaining, I suppose. 
                *  I just perform an assignment to variables from the i-th
                *  element inside the array
                */
                $symbol = $obj['result'][$i]['MarketName'];
                $ask_price = $obj['result'][$i]['Ask'];
                $volumes = $obj['result'][$i]['Volume'];

                $coins[$i] = array(
                    "symbol" => $symbol,
                    "ask_price" => $ask_price,
                    "volumes" => $volumes,
                    "%_var_price" => 0,
                    "%_var_volumes" => 0,
                ); 

            }           


        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    相关资源
    最近更新 更多