3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( 'q0data' => '{"question":"chicken","modal":"ought to","probability":"high","utilitygain":"low","subcondition":"Edward","rt":2975,"response":"agree","freeResponse":"wvwvwewe"}', 'q1data' => '{"question":"procrastinate","modal":"ought to","probability":"low","utilitygain":"high","subcondition":"accept","rt":2603,"response":"disagree","freeResponse":"vwevewvewvewvew"}', 'language' => 'vewoievwnpio', 'payment' => 'ceeqqecqeq' ); function parseTrialData ($trialData) { $trialData = substr($trialData, 1, -1); // get rid of leading '{' and trailing '}' $trialData = explode(',', $trialData); // 'explode' is PHP for 'split' // i.e., separate the string into an array of strings of form 'key:value' $parsedTrialData = array(); foreach ($trialData as $kv) { $exploded = explode(':', $kv); $parsedTrialData[$exploded[0]] = $exploded[1]; } return $parsedTrialData; } $headerItems = array(); $trialData = array(); foreach (array_keys($arr) as $key) { $value = $arr[$key]; if (substr($value[1], 0, 1) == '{' and substr($value, -1) == '{') { // if the data starts and ends with '{', '}' then it's a complex data object representing a single trial. // so, add the key-value pair to $trialData array to await further processing $trialData[$key] = $value; } else { // otherwise, it's data shared across all trials; so add the pair to the $headerItems array $headerItems[$key] = $value; } } $header = ""; // for new files, make header showing variable names $globalHeader = ""; foreach (array_keys($headerItems) as $key) { $globalHeader .= $key . ","; } $localHeader = ""; if (count($trialData) !== 0) { $parsedSampleTrialData = parseTrialData(array_values($trialData)[0]); foreach (array_keys($parsedSampleTrialData) as $key) { $localHeader .= $key . ","; } } $header .= $globalHeader . $localHeader; $header = substr($header, 0, -1) . "\n"; foreach ($array_keys($headerItems) as $key) { echo $key; } ?>

preferences:
63.8 ms | 402 KiB | 5 Q