同一阶段所有课程处理

/**
 * 阶段课程处理
 * $array 订单原数组,一个用户有多少订单,一个订单有多个相同阶段,同一个阶段有不能同的课程,这里处理区分同一个阶段的所有课程并返回一个数组
 * $type 类型
 */
public function stageArray($array, $type)
{
    //所有阶段
    $cs = CourseStageModel::select()->toArray();
    $list = array();
    if (!empty($array)) {
        $data = array();
        foreach ($array as $k => $v) {
            $data[$v['stage_id']]['stage']['is_load'] = false;
            $data[$v['stage_id']]['stage']['stage_id'] = $v['stage_id'];
            $data[$v['stage_id']]['stage']['oc_id'] = $v['oc_id'];
            $data[$v['stage_id']]['stage']['name'] = $v['stage_name'];
            $data[$v['stage_id']]['stage']['is_practice'] = false;
            if (!isset($data[$v['stage_id']])) { //如果不存在就写入
                $data[$v['stage_id']]['course'][] = $v;
            } else {
                $data[$v['stage_id']]['course'][] = $v; //存在就先存起来
            }
        }
        $list = array_values($data); //取出键值,原格式:['1':{'id':1},'2':{'id':2}] 取出后 [{'id':1},{'id':2}]
        $css = array_column($cs, 'id');
        //从多维数组中分离出一列二维数组
        $s = array_column($list, 'stage');
        $ss = array_column($s, 'stage_id');

        $a = array_diff($css, $ss); //两个数组的差集
        if (!empty($a)) {
            foreach ($cs as $k => $v) {
                foreach ($a as $ks => $vs) {
                    if ($cs[$k]['id'] == $vs) {
                        $stage['stage']['is_load'] = false;
                        $stage['stage']['stage_id'] = $cs[$k]['id'];
                        $stage['stage']['name'] = $cs[$k]['stage_name'];
                        $stage['stage']['sum_course_score'] = 0; //课程总时长
                        $stage['stage']['course_duration'] = 0;
                        $stage['stage']['course_avg'] = 0;
                        $stage['stage']['is_practice'] = false;
                        $stage['course'] = array();
                        $list[] = $stage;
                    }
                }
            }
        }

        $id = array(); //多维数组对id排序
        foreach ($list as $key => $row) {
            $id[$key] = $row['stage']['stage_id'];
        }

        //所有阶段课程总时长
        foreach ($list as $k => $v) {
            $sum_course_score = 0;
            $course = $v['course'];
            if (!empty($course)) {
                foreach ($course as $ks => $vs) {
                    $sum_course_score += $vs['course_duration'];
                }
            }
            $list[$k]['stage']['sum_course_score'] = $sum_course_score / 60;
        }

        array_multisort($id, SORT_ASC, $list); //多维数组排序
    } else {
        //空阶段处理
        foreach ($cs as $k => $v) {
            $stage['stage']['is_load'] = false;
            $stage['stage']['stage_id'] = $cs[$k]['id'];
            $stage['stage']['name'] = $cs[$k]['stage_name'];
            $stage['stage']['course_duration'] = 0;
            $stage['stage']['course_avg'] = 0;
            $stage['stage']['sum_course_score'] = 0;
            $stage['stage']['is_practice'] = false;
            $stage['course'] = array();
            $list[] = $stage;
        }
    }
    return $list;
}

你可能感兴趣的:(php数组处理)