doulian1852
2017-06-29 12:54
浏览 88
已采纳

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

Here Is my array i.e. $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'])
                );

output of above array :

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
                            )

                    )

            )

    )

)

My database field store question_options like 1,2,3,4

enter image description here

I want the array like

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

                    )

            )

    )

)

Please help me to solve out this issue.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzi4724 2017-06-29 13:32
    已采纳

    Try this.

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

    I think this will Help you.

    点赞 打赏 评论
  • duanjue9296 2017-06-29 13:00

    Declare an array as below :

    $meaningArray = array(
        1 => 'Yes',
        2 => 'No',
        3 => 'N/A',
        4 => 'Comment'
    );
    

    And use :

    'question_options' => explode(',',$meaningArray[$question_value['question_options']]);
    

    Instead of :

    'question_options' => explode(',',$question_value['question_options'])
    
    点赞 打赏 评论

相关推荐 更多相似问题