array("Home/ office delivery","Pick up in store","Pick up in convenience points through locker"),"Distance" => array("< 1 Km","1 - 2.5 Km","2.5 - 5 Km",">5 Km"),"Delivery lead time" => array("> Two days","Two days","Next day","Same day"),"Delivery time window" => array("Early mornings","Working hours","Late night","Anytime"),"Delivery cost" => array("$5","$3","$1","Free"));
$restrictionarray = array(array(array("Pick up method","Home/ office delivery"),array("Distance","1 - 2.5 Km")),array(array("Pick up method","Home/ office delivery"),array("Distance","2.5 - 5 Km")),array(array("Pick up method","Home/ office delivery"),array("Distance",">5 Km")),array(array("Pick up method","Pick up in store"),array("Delivery time window","Early mornings")),array(array("Pick up method","Pick up in store"),array("Delivery time window","Late night")),array(array("Pick up method","Pick up in store"),array("Delivery time window","Anytime")),array(array("Pick up method","Pick up in convenience points through locker"),array("Delivery time window","Early mornings")),array(array("Pick up method","Pick up in convenience points through locker"),array("Delivery time window","Working hours")),array(array("Pick up method","Pick up in convenience points through locker"),array("Delivery time window","Late night")));
$probabilityarray = array("Pick up method" => array(0.333333333333,0.333333333333,0.333333333333),"Distance" => array(0.25,0.25,0.25,0.25),"Delivery lead time" => array(0.25,0.25,0.25,0.25),"Delivery time window" => array(0.25,0.25,0.25,0.25),"Delivery cost" => array(0.25,0.25,0.25,0.25));
// Indicator for whether weighted randomization should be enabled or not
$weighted = 1;
// K = Number of tasks displayed to the respondent
$K = 6;
// N = Number of profiles displayed in each task
$N = 3;
// num_attributes = Number of Attributes in the Array
$num_attributes = count($featurearray);
$featureArrayNew = $featurearray;
// 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;
}
}
}
// Return the array back to Qualtrics
print json_encode($returnarray);
?>