2016-09-07 66 views
1

我被困在生成字符串多維數組中,我需要你的幫助。 這是字符串(JSON):從爆炸字符串生成多維數組

{ 
"API_OVERVIEW" : "Overview", 
"API_OVERVIEW_CON1" : "API v1 offers a simple REST API to retrieve information about our markets. The API responses are JSON encoded arrays. Please be aware that calls to the API are rate limited to 10 requests per second, any requests exceeding this rate will be met with a HTTP 503 response.", 
"API_REFERENCE" : "API Reference", 
"API_MARKET_SUMMARY" : "Market Summary", 
"API_MARKET_STATS" : "Market Stats", 
"API_MARKET_TRADES" : "Market Trades", 
"API_MARKET_ORDERS" : "Market Orders", 
"API_MARKET_CHARTDATA" : "Market Chart Data", 
} 

現在我需要通過「_」爆炸的key並將其轉換爲多維數組,然後在最後,我需要設置的值。 輸出應該是這樣的:

"API" => 
    [ 
     "MARKET" => 
      ["SUMMARY" => "Market Summary"], 
      ["STATS" => "Market STATS"] 
      ... 
    ] 
"ANOTHER STRING" => 
    [ 
     .... 
    ] 

目前,我得到這個:

array(1) { 
    ["API"]=> 
    array(1) { 
     ["MARKET"]=> 
     array(1) { 
     ["SUMMARY"]=> 
     string(14) "Market Summary" 
     } 
    } 
    } 
    [8]=> 
    array(1) { 
    ["API"]=> 
    array(1) { 
     ["MARKET"]=> 
     array(1) { 
     ["STATS"]=> 
     string(12) "Market Stats" 
     } 
    } 
    }... 

這是我的代碼:

$results = []; 
foreach($data as $key => $value){ 
    $result = []; 
    $exploded = explode('_', $key); 
    $path = &$result; 

    $counter = 1; 
    foreach($exploded as $explodedpart){ 
     if(!array_key_exists($explodedpart, $path)){ 
      if($counter == count($exploded)){ 
       $path[$explodedpart] = $value; 
      }else{ 
       $path[$explodedpart] = array(); 
      } 
     } 
     $path = &$path[$explodedpart]; 

     $counter++; 
    } 

    array_push($results, $result); 
} 

return $results; 

理念從這個答案採取:https://stackoverflow.com/a/8993400/1672261

+1

請發佈您的json字符串。 – Amy

+0

請發佈您的精確json字符串 –

+0

它已更新,它實際上是Angular的翻譯文件,只是一個短的文件 – Alen

回答

1

在您的代碼取代array_push($results, $result);替換爲$results = array_merge_recursive($results, $result);

結果將是

{ 
    "API":{ 
     "OVERVIEW":{ 
     "0":"Overview", 
     "CON1":"API v1 offers a simple REST API to retrieve information about our markets. The API responses are JSON encoded arrays. Please be aware that calls to the API are rate limited to 10 requests per second, any requests exceeding this rate will be met with a HTTP 503 response." 
     }, 
     "REFERENCE":"API Reference", 
     "MARKET":{ 
     "SUMMARY":"Market Summary", 
     "STATS":"Market Stats", 
     "TRADES":"Market Trades", 
     "ORDERS":"Market Orders", 
     "CHARTDATA":"Market Chart Data" 
     } 
    } 
} 

正如你看到的不知道你想怎麼處理API_OVERVIEW & API_OVERVIEW_CON1。但我希望這會以某種方式幫助你。

也嘗試了一些不同的東西。這也輸出相同的結果

$results = array(); 

foreach($data as $key => $value) { 
    // Get all the keys 
    $keyParts = explode('_', $key); 

    // Make a JSON string 
    // Like this {"A" : { "B" : "c" } } 
    $jsonStr = '{"'; // Open brackets 
    $jsonStr .= implode('":{"', $keyParts); 
    $jsonStr .= '":"'.$value.'"'; // End of the str 

    // Close brackets 
    // No of close brackets = No of keys 
    for($i = 0; $i < count($keyParts); $i++) { 
     $jsonStr .= "}"; 
    } 

    // Convert the JSON string into array 
    $tmpResults = json_decode($jsonStr, true); 

    // Merge into final results 
    $results = array_merge_recursive($results, $tmpResults); 
} 
+0

其實我想到了這一分鐘前:)但謝謝你的回答,我會接受這個答案 – Alen