gpt4 book ai didi

php - 如何获取数组中的逗号分隔符(,)值?

转载 作者:行者123 更新时间:2023-12-03 18:46:11 26 4
gpt4 key购买 nike

这是我的数组,即 $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

enter image description here

我想要这样的数组

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'
)

)

)

)

)

请帮我解决这个问题。

最佳答案

试试这个。

$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));

我认为这会对您有所帮助。

关于php - 如何获取数组中的逗号分隔符(,)值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44825723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com