php - Generate multidimensional array out of exploded string -


i'm stuck @ generating multidimensional array out of string , need help. string (json):

{ "api_overview" : "overview", "api_overview_con1" : "api v1 offers simple rest api retrieve information our markets. api responses json encoded arrays. please aware calls api rate limited 10 requests per second, requests exceeding rate met 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", } 

now need explode key "_" , convert multidimensional array, @ end need set value. output should this:

"api" =>     [         "market" =>              ["summary" => "market summary"],             ["stats" => "market stats"]              ...     ] "another string" =>     [        ....     ] 

currently this:

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"       }     }   }... 

this code:

$results = []; foreach($data $key => $value){     $result = [];     $exploded = explode('_', $key);     $path = &$result;      $counter = 1;     foreach($exploded $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; 

idea taken answer: https://stackoverflow.com/a/8993400/1672261

in code instead of array_push($results, $result); replace $results = array_merge_recursive($results, $result);

the results be

{    "api":{       "overview":{          "0":"overview",          "con1":"api v1 offers simple rest api retrieve information our markets. api responses json encoded arrays. please aware calls api rate limited 10 requests per second, requests exceeding rate met http 503 response."       },       "reference":"api reference",       "market":{          "summary":"market summary",          "stats":"market stats",          "trades":"market trades",          "orders":"market orders",          "chartdata":"market chart data"       }    } } 

as see not sure how want handle api_overview & api_overview_con1. hope in way.

also tried different. outputs same results

$results = array();  foreach($data $key => $value) {     // keys     $keyparts = explode('_', $key);      // make json string     // {"a" : { "b" : "c" } }     $jsonstr = '{"'; // open brackets     $jsonstr .= implode('":{"', $keyparts);     $jsonstr .= '":"'.$value.'"'; // end of str      // close brackets     // no of close brackets = no of keys     for($i = 0; $i < count($keyparts); $i++) {         $jsonstr .= "}";     }      // convert json string array     $tmpresults = json_decode($jsonstr, true);      // merge final results     $results = array_merge_recursive($results, $tmpresults); } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -