Hallo
Ich mache gerade Arbeits-Ferien in Polen und ein Mitbewohner im Hostel hat eine PHP Frage. Ich bin aber kein Programierer also habe ich mir gedacht, ich stelle das Problem mal ins Heise Forum, hier gibts bestimmt einen PHP Crack der Ihm weiterhelfen kann ;-)
Vielen Dank!
Hier sein Problem:
when the php file is executed on an external platform it loses some Polish/Turkish/Hungarian specific letters even though it is saved in UTF-8.
Hier der PHP Code:
<?php
// Code to randomly generate conjoint profiles to send to a Qualtrics instance
// Terminology clarification:
// Task = Set of choices presented to respondent in a single screen (i.e. pair of candidates)
// Profile = Single list of attributes in a given task (i.e. candidate)
// Attribute = Category characterized by a set of levels (i.e. education level)
// Level = Value that an attribute can take in a particular choice task (i.e. "no formal education")
// Attributes and Levels stored in a 2-dimensional Array
// Function to generate weighted random numbers
function weighted_randomize($prob_array, $at_key)
{
$prob_list = $prob_array[$at_key];
// Create an array containing cutpoints for randomization
$cumul_prob = array();
$cumulative = 0.0;
for ($i=0; $i<count($prob_list); $i++){
$cumul_prob[$i] = $cumulative;
$cumulative = $cumulative + floatval($prob_list[$i]);
}
// Generate a uniform random floating point value between 0.0 and 1.0
$unif_rand = mt_rand() / mt_getrandmax();
// Figure out which integer should be returned
$outInt = 0;
for ($k = 0; $k < count($cumul_prob); $k++){
if ($cumul_prob[$k] <= $unif_rand){
$outInt = $k + 1;
}
}
return($outInt);
}
$featurearray = array("Ozellikler" => array("T¸rkiye'de dËrd¸nc¸ s?n?f? bitirmeye denk","T¸rkiye'de sekizinci s?n?f? bitirmeye denk","T¸rkiye'de liseyi bitirmeye denk","T¸rkiye'de ¸niversiteyi veya y¸ksekokulu bitirmeye denk","T¸rkiye'de y¸ksek lisans derecesine denk","HiĂbir resmi e?itimim yok"),"Cinsiyet" => array("Erkek","Kad?n"),"Dil" => array("Bu aday kabul iĂin m¸lakat s?ras?nda bozuk bir T¸rkĂe konu?tu","Bu aday kabul iĂin m¸lakat s?ras?nda T¸rkĂe konu?maya Ăal??t? ancak ba?aramad?","Bu aday kabul iĂin m¸lakat s?ras?nda ak?c? bir T¸rkĂe konu?tu","u aday kabul iĂin m¸lakat s?ras?nda terc¸man arac?l???yla konu?tu"),"GËĂ Sebebi" => array("?nsani Sebepler","Ailenin yeniden bir araya gelmesi","Ekonomik Sebepler","T¸rkiye'de daha iyi bir i? bulabilmek iĂin"),"Orada yerli bir dan??manla bir dan??manl?k program?na kat?laca??m" => array("Evet","Hayir"),"Men?ei âšlke" => array("Sudan","Afganistan","Suriye","Eritre","Nijerya","Pakistan","S?rbistan","Kosova"),"Meslek" => array("Garson","?n?aat ??Ăisi","á?retmen","Doktor","IT Uzman?","Tarim ??Ăisi","á?renci"),"Mesleki Deneyim" => array("HiĂbir mesleki e?itim ya da tecr¸bem yok"," iki y?l","be? y?l","Be? y?ldan fazla"),"Dini" => array("Ateist","M¸sl¸man","Hristiyan","Di?er"),"Calisma Planlari" => array("T¸rk bir i?verenle anla?mam var","T¸rk bir i?verenle anla?mam yok ancak m¸lakatlar yapt?m","T¸rkiye'ye geldikten sonra i? arayaca??m","?u anda i? aramak gibi bir plan?m yok"),"AvrupaĂya daha Ënceki geli?leri" => array("HiĂ","Bir kez tatil iĂin","Birden fazla kez tatil iĂin","?? gezisi","AvrupaĂda ya?ad?m"));
$restrictionarray = array(array(array("Ozellikler","HiĂbir resmi e?itimim yok"),array("Meslek","á?retmen")),array(array("Ozellikler","HiĂbir resmi e?itimim yok"),array("Meslek","Doktor")),array(array("Ozellikler","HiĂbir resmi e?itimim yok"),array("Meslek","IT Uzman?")),array(array("Mesleki Deneyim","HiĂbir mesleki e?itim ya da tecr¸bem yok"),array("Meslek","á?retmen")),array(array("Mesleki Deneyim","HiĂbir mesleki e?itim ya da tecr¸bem yok"),array("Meslek","Doktor")),array(array("Mesleki Deneyim","HiĂbir mesleki e?itim ya da tecr¸bem yok"),array("Meslek","IT Uzman?")),array(array("Dini","M¸sl¸man"),array("Men?ei âšlke","Sirbistan")),array(array("Ozellikler","T¸rkiye'de dËrd¸nc¸ s?n?f? bitirmeye denk"),array("Meslek","á?retmen")),array(array("Ozellikler","T¸rkiye'de dËrd¸nc¸ s?n?f? bitirmeye denk"),array("Meslek","Doktor")),array(array("Ozellikler","T¸rkiye'de dËrd¸nc¸ s?n?f? bitirmeye denk"),array("Meslek","IT Uzman?")),array(array("Ozellikler","T¸rkiye'de sekizinci s?n?f? bitirmeye denk"),array("Meslek","á?retmen")),array(array("Ozellikler","T¸rkiye'de sekizinci s?n?f? bitirmeye denk"),array("Meslek","Doktor")),array(array("Ozellikler","T¸rkiye'de sekizinci s?n?f? bitirmeye denk"),array("Meslek","IT Uzman?")),array(array("Ozellikler","T¸rkiye'de liseyi bitirmeye denk"),array("Meslek","á?retmen")),array(array("Ozellikler","T¸rkiye'de liseyi bitirmeye denk"),array("Meslek","Doktor")),array(array("Ozellikler","T¸rkiye'de liseyi bitirmeye denk"),array("Meslek","IT Uzman?")));
// Indicator for whether weighted randomization should be enabled or not
$weighted = 0;
// K = Number of tasks displayed to the respondent
$K = 5;
// N = Number of profiles displayed in each task
$N = 2;
// num_attributes = Number of Attributes in the Array
$num_attributes = count($featurearray);
$attrconstraintarray = array();
// Re-randomize the $featurearray
// Place the $featurearray keys into a new array
$featureArrayKeys = array();
$incr = 0;
foreach($featurearray as $attribute => $levels){
$featureArrayKeys[$incr] = $attribute;
$incr = $incr + 1;
}
// Backup $featureArrayKeys
$featureArrayKeysBackup = $featureArrayKeys;
// If order randomization constraints exist, drop all of the non-free attributes
if (count($attrconstraintarray) != 0){
foreach ($attrconstraintarray as $constraints){
if (count($constraints) > 1){
for ($p = 1; $p < count($constraints); $p++){
if (in_array($constraints[$p], $featureArrayKeys)){
$remkey = array_search($constraints[$p],$featureArrayKeys);
unset($featureArrayKeys[$remkey]);
}
}
}
}
}
// Re-set the array key indices
$featureArrayKeys = array_values($featureArrayKeys);
// Re-randomize the $featurearray keys
shuffle($featureArrayKeys);
// Re-insert the non-free attributes constrained by $attrconstraintarray
if (count($attrconstraintarray) != 0){
foreach ($attrconstraintarray as $constraints){
if (count($constraints) > 1){
$insertloc = $constraints[0];
if (in_array($insertloc, $featureArrayKeys)){
$insert_block = array($insertloc);
for ($p = 1; $p < count($constraints); $p++){
if (in_array($constraints[$p], $featureArrayKeysBackup)){
array_push($insert_block, $constraints[$p]);
}
}
$begin_index = array_search($insertloc, $featureArrayKeys);
array_splice($featureArrayKeys, $begin_index, 1, $insert_block);
}
}
}
}
// Re-generate the new $featurearray - label it $featureArrayNew
$featureArrayNew = array();
foreach($featureArrayKeys as $key){
$featureArrayNew[$key] = $featurearray[$key];
}
// Initialize the array returned to the user
// Naming Convention
// Level Name: F-[task number]-[profile number]-[attribute number]
// Attribute Name: F-[task number]-[attribute number]
// Example: F-1-3-2, Returns the level corresponding to Task 1, Profile 3, Attribute 2
// F-3-3, Returns the attribute name corresponding to Task 3, Attribute 3
$returnarray = array();
// For each task $p
for($p = 1; $p <= $K; $p++){
// For each profile $i
for($i = 1; $i <= $N; $i++){
// Repeat until non-restricted profile generated
$complete = False;
while ($complete == False){
// Create a count for $attributes to be incremented in the next loop
$attr = 0;
// Create a dictionary to hold profile's attributes
$profile_dict = array();
// For each attribute $attribute and level array $levels in task $p
foreach($featureArrayNew as $attribute => $levels){
// Increment attribute count
$attr = $attr + 1;
// Create key for attribute name
$attr_key = "F-" . (string)$p . "-" . (string)$attr;
// Store attribute name in $returnarray
$returnarray[$attr_key] = $attribute;
// Get length of $levels array
$num_levels = count($levels);
// Randomly select one of the level indices
if ($weighted == 1){
$level_index = weighted_randomize($probabilityarray, $attribute) - 1;
}else{
$level_index = mt_rand(1,$num_levels) - 1;
}
// Pull out the selected level
$chosen_level = $levels[$level_index];
// Store selected level in $profileDict
$profile_dict[$attribute] = $chosen_level;
// Create key for level in $returnarray
$level_key = "F-" . (string)$p . "-" . (string)$i . "-" . (string)$attr;
// Store selected level in $returnarray
$returnarray[$level_key] = $chosen_level;
}
$clear = True;
// Cycle through restrictions to confirm/reject profile
if(count($restrictionarray) != 0){
foreach($restrictionarray as $restriction){
$false = 1;
foreach($restriction as $pair){
if ($profile_dict[$pair[0]] == $pair[1]){
$false = $false*1;
}else{
$false = $false*0;
}
}
if ($false == 1){
$clear = False;
}
}
}
$complete = $clear;
}
}
}
function array_utf8_encode($dat)
{
if (is_string($dat))
return utf8_encode($dat);
if (!is_array($dat))
return $dat;
$ret = array();
foreach ($dat as $i => $d)
$ret[$i] = array_utf8_encode($d);
return $ret;
}
header('Content-Type: application/json');
// Return the array back to Qualtrics
print json_encode(array_utf8_encode($returnarray));
?>