这是我的数组,即$ data。
function generateQuestion($userdata) {
$module_name = $userdata->module_name;
$module = new Module();
$module->data['*'] = '';
$module->where = " module_name = '" . $module_name . "' and is_delete='0'";
$module->action = "get";
$module_res = $module->process();
$data = array();
if($module_res['count'] > 0) {
foreach ($module_res['res'] as $module_key => $module_value) {
$cat = new Category();
$cat->data['*'] = '';
$cat->where = "que_module_id='".$module_value['module_id']."' and is_delete='0'";
$cat->action = "get";
$cat_res = $cat->process();
foreach ($cat_res['res'] as $cat_key => $cat_value) {
$question = new Question();
$question->data['*'] = '';
$question->where = "question_cat_id='".$cat_value['que_cat_id']."' and is_delete='0'";
$question->action = "get";
$question_res = $question->process();
foreach ($question_res['res'] as $question_key => $question_value) {
$que_ans = '';
if($question_value['question_ans'] == 1) {
$que_ans = 'Yes';
} elseif($question_value['question_ans'] == 2) {
$que_ans = 'No';
} else {
$que_ans = 'N/A';
}
$options = explode(',',$question_value['question_options']);
$data[$module_value['module_name']][$cat_value['que_cat_id']][$question_value['question_id']] = array(
'question_name' => $question_value['question_name'],
'question_ans' => $que_ans,
'question_ans_comments' => $question_value['question_ans_comments'] == ''?'None':$question_value['question_ans_comments'],
'question_options' => explode(',',$question_value['question_options'])
);
}
}
}
print_r($data);die;
//echo json_encode(array("data"=>$data,"status" => "success", 'message' => 'Question Listing'));
} else {
echo json_encode(array("status" => "error", 'message' => 'Module Not Found'));
}
}
$data[$module_value['module_name']][$cat_value['que_cat_id']]
[$question_value['question_id']] = array(
'question_name' => $question_value['question_name'],
'question_ans' => $que_ans,
'question_ans_comments' => $question_value['question_ans_comments'] == ''?'None':$question_value['question_ans_comments'],
'question_options' => explode(',',$question_value['question_options'])
);
以上数组的输出:
Array
(
[Prestart Hand Tools/Electric Power Tool Safety Checks] => Array
(
[3] => Array
(
[1] => Array
(
[question_name] => machine runs correctly no
abnormal noise
[question_ans] => No
[question_ans_comments] => None
[question_options] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
[4] => Array
(
[question_name] => abc
[question_ans] => Yes
[question_ans_comments] => None
[question_options] => Array
(
[0] => 1
)
)
)
[5] => Array
(
[2] => Array
(
[question_name] => machine runs correctly no abnormal noise
[question_ans] => No
[question_ans_comments] => None
[question_options] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
)
[6] => Array
(
[3] => Array
(
[question_name] => machine runs correctly no abnormal noise
[question_ans] => No
[question_ans_comments] => None
[question_options] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
)
)
)
我的数据库字段存储question_options,如1,2,3,4
我希望数组像
Array
(
[Prestart Hand Tools/Electric Power Tool Safety Checks] => Array
(
[3] => Array
(
[1] => Array
(
[question_name] => machine runs correctly no
abnormal noise
[question_ans] => No
[question_ans_comments] => None
[question_options] => Array
(
[1] => 'Yes'
[2] => 'No'
[3] => 'N/a'
[4] => 'comments'
)
)
[4] => Array
(
[question_name] => abc
[question_ans] => Yes
[question_ans_comments] => None
[question_options] => Array
(
[1] => 'Yes'
)
)
)
[5] => Array
(
[2] => Array
(
[question_name] => machine runs correctly no
abnormal noise
[question_ans] => No
[question_ans_comments] => None
[question_options] => Array
(
[1] => 'Yes'
[2] => 'No'
[3] => 'N/a'
[4] => 'comments'
)
)
)
[6] => Array
(
[3] => Array
(
[question_name] => machine runs correctly no
abnormal noise
[question_ans] => No
[question_ans_comments] => None
[question_options] => Array
(
[1] => 'Yes'
[2] => 'No'
[3] => 'N/a'
[4] => 'comments'
)
)
)
)
)
请帮我解决这个问题。
答案 0 :(得分:5)
试试这个。
$options = array(1,2,4); // array of explode(',',$question_value['question_options'])
function getOptions($options){
$temparray = array();
$option_fix_array = array(
1 => 'Yes',
2 => 'NO',
3 => 'N/A',
4 => 'Comments',
);
foreach($options as $value){
if(array_key_exists($value,$option_fix_array)){
$temparray[$value] = $option_fix_array[$value];
}
}
return $temparray;
}
print_r(getOptions($options));
我认为这会帮助你。
答案 1 :(得分:1)
声明一个数组如下:
$meaningArray = array(
1 => 'Yes',
2 => 'No',
3 => 'N/A',
4 => 'Comment'
);
并使用:
'question_options' => explode(',',$meaningArray[$question_value['question_options']]);
而不是:
'question_options' => explode(',',$question_value['question_options'])