生成多维数组的爆炸字符串



我被困在生成多维数组的字符串,我需要你的帮助。这是字符串(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

在你的代码中用$results = array_merge_recursive($results, $result);代替array_push($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);
}

相关内容

  • 没有找到相关文章

最新更新