2017-12-01 21 views
-1

서버에서 json 배열을 실시간으로 받고 있습니다. 이 구조입니다 :JSON 중첩 데이터를 PHP 배열 배열로 변환

나는 다음과 같은 매트릭스 구조를 가지고 있어야 배열의 PHP 배열로 입력에서 얻을 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”}] 
} 

:

| 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"를 포함 요소에서 배열의 배열을 채우려. 즉, 배열의 배열에 삽입 할 모든 객체에 "BTC-what what else"라는 MarketName이 있는지 루프를 통해 확인해야합니다.

친절하게 도와 줄 수 있습니까? 나는 PHP에 아주 익숙하다.

+1

이'PHP 매뉴얼 – RiggsFolly

+3

에서'json_decode()를 찾아 : 어쨌든, 여기 내 솔루션입니다. 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, 
       ); 

      }   


     }