<?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 (mb_substr($value, 0, 1) == '{' and mb_substr($value, strlen($value) - 1, 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 .= substr($key, 1, -1) . ",";
}
}
$header .= $globalHeader . $localHeader;
$header = substr($header, 0, -1) . "\n";
$data = "";
// one line for each trial data point, starting with header items, then trial data
foreach (array_values($trialData) as $trialStr) {
$trialParsed = parseTrialData($trialStr);
foreach (array_values($trialParsed) as $trialDataPoint) {
$data .= $trialDataPoint . ",";
}
$data = mb_substr(data, 0, -1) . "\n";
}
echo $data;
?>
Fatal error: Uncaught Error: Undefined constant "data" in /in/oivqg:61
Stack trace:
#0 {main}
thrown in /in/oivqg on line 61
Process exited with code 255.
Warning: Use of undefined constant data - assumed 'data' (this will throw an Error in a future version of PHP) in /in/oivqg on line 61
Warning: Use of undefined constant data - assumed 'data' (this will throw an Error in a future version of PHP) in /in/oivqg on line 61
dat
Notice: Use of undefined constant data - assumed 'data' in /in/oivqg on line 61
Notice: Use of undefined constant data - assumed 'data' in /in/oivqg on line 61
dat