如何在php中打印多维数组?

时间:2017-07-04 05:05:48

标签: php arrays

使用下面的代码我必须打印我的数组:

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 = "getSingleRow";
    $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_res['res']['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']);
                $temparray = array();
                $meaningArray = array(
                    1 => 'Yes',
                    2 => 'No',
                    3 => 'N/A',
                    4 => 'Comment'
                );
                foreach ($options as $value) {
                    if (array_key_exists($value, $meaningArray)) {
                        $temparray[$value] = $meaningArray[$value];
                    }
                }
                $val = array_flip(explode(',', $question_value['question_options']));
                $all = array();
                $data[$module_res['res']['module_name']][$cat_value['que_cat_name']] = array(
                    'que_cat_id' => $cat_value['que_cat_id'],
                    $all[$question_key] = array(
                        'question_id' => $question_value['question_id'],
                        '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' => $temparray
                    )
                );
            }
        }
          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'));
    }
}

这是我的数组如下所示:

     Array
    (
      [Prestart Hand Tools/Electric Power Tool Safety Checks] => Array
      (
        [visual check] => Array
            (
                [que_cat_id] => 3
                [0] => Array
                    (
                        [question_id] => 4
                        [question_name] => abc
                        [question_ans] => Yes
                        [question_ans_comments] => None
                        [question_options] => Array
                            (
                                [1] => Yes
                            )
                    )
            )

        [operational check] => Array
            (
                [que_cat_id] => 5
                [0] => Array
                    (
                        [question_id] => 2
                        [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] => Comment
                            )
                    )
            )

        [operating instruction] => Array
            (
                [que_cat_id] => 6
                [0] => Array
                    (
                        [question_id] => 3
                        [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] => Comment
                            )
                    )
            )
    )
 )

但我想要如下所示的数组:

    Array
    (
      [Prestart Hand Tools/Electric Power Tool Safety Checks] => Array
      (
        [visual check] => Array
            (
                [que_cat_id] => 3
                [0] => Array
                    (
                        [question_id] => 4
                        [question_name] => abc
                        [question_ans] => Yes
                        [question_ans_comments] => None
                        [question_options] => Array
                            (
                                [1] => Yes
                            )

                    )
                [1] => Array
                    (
                        [question_id] => 1
                        [question_name] => machine runs correctly no 
                        abnormal noise
                        [question_ans] => Yes
                        [question_ans_comments] => None
                        [question_options] => Array
                            (
                                [1] => Yes
                                [2] => No
                                [3] => N/A
                                [4] => Comment
                            )
                    ) 
               [2] => Array
                    (
                        [question_id] => 2
                        [question_name] => machine runs correctly no 
                        abnormal noise
                        [question_ans] => Yes
                        [question_ans_comments] => None
                        [question_options] => Array
                            (
                                [1] => Yes
                                [2] => No
                                [3] => N/A
                                [4] => Comment
                            )
                    ) 
            )

        [operational check] => Array
            (
                [que_cat_id] => 5
                [0] => Array
                    (
                        [question_id] => 2
                        [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] => Comment
                            )
                    )
            )

        [operating instruction] => Array
            (
                [que_cat_id] => 6
                [0] => Array
                    (
                        [question_id] => 3
                        [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] => Comment
                            )
                    )
            )
    )
 )

在上面的数组中,类别名称目视检查有3个问题,即数组键0,1,2,如上所示,如何打印此数组?

请帮我解决这个问题

0 个答案:

没有答案