2017-12-01 8 views
-1

サーバーからJSON配列をリアルタイムで受信しています。 これは構造体である:私がする必要がどのようなJSONネストされたデータをPHP配列の配列に変換する

{ 
"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”}] 
} 

は、私は、次のマトリックス構造を有していなければならない配列のPHPの配列に入力してもらうJSONからいくつかの値を代入されます。

| 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に新しいです。

+1

'json_decode()あなたがしようとしたら、それは厳しい取得するときに' PHPマニュアル – RiggsFolly

+3

に、私たちはあなたを助けるを見上げます。 1. json_decodeサーバーの結果 2.loop over – imox

+0

あなたのコードはどこですか? @imoxのように何かを試してみて – Krish

答えて

0

問題を解決しました。上記のコメントで誰かが言ったように、私はちょうどコードで少し遊ばなければなりませんでした。 とにかく、ここに私の解決策だ:

$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, 
       ); 

      }   


     } 
関連する問題