Home >Backend Development >PHP Tutorial >PHP implements the merging of a two-dimensional array
1. This problem is mainly the merging of PH operation P two-dimensional array;
2. The initial array is as follows:
<code>$course = array( array( 'course_name' => '高等数学', 'major_name' => '计算机专业', 'school_name' => '某A高校' ), array( 'course_name' => '高等数学', 'major_name' => '物理专业', 'school_name' => '某A高校' ), array( 'course_name' => '高等数学', 'major_name' => '日语专业', 'school_name' => '某A高校' ), array( 'course_name' => '马克思概论', 'major_name' => '计算机专业', 'school_name' => '某B高校' ) )</code>
3. I hope to get the following array:
<code>$course = array( array( 'course_name' => '高等数学', 'major_name' => array('计算机专业','物理专业','日语专业'), 'school_name' => '某A高校' ), array( 'course_name' => '马克思概论', 'major_name' => array('计算机专业'), 'school_name' => '某B高校' ) )</code>
4. This is my first time asking a question; I hope I can get a reply. I'm not talented, I'm confused by this question, (I couldn't find a good answer, so I reached out once) (Urgent, urgent)
1. This problem is mainly the merging of PH operation P two-dimensional array;
2. The initial array is as follows:
<code>$course = array( array( 'course_name' => '高等数学', 'major_name' => '计算机专业', 'school_name' => '某A高校' ), array( 'course_name' => '高等数学', 'major_name' => '物理专业', 'school_name' => '某A高校' ), array( 'course_name' => '高等数学', 'major_name' => '日语专业', 'school_name' => '某A高校' ), array( 'course_name' => '马克思概论', 'major_name' => '计算机专业', 'school_name' => '某B高校' ) )</code>
3. I hope to get the following array:
<code>$course = array( array( 'course_name' => '高等数学', 'major_name' => array('计算机专业','物理专业','日语专业'), 'school_name' => '某A高校' ), array( 'course_name' => '马克思概论', 'major_name' => array('计算机专业'), 'school_name' => '某B高校' ) )</code>
4. This is my first time asking a question; I hope I can get a reply. I'm not talented, I'm confused by this question, (I couldn't find a good answer, so I reached out once) (Urgent, urgent)
First of all, the dimension of your data is as follows:
"College_Course": [Major 1, [Major 2, Major 3]]
So the only difference must be College_Course
<code>// 上接你的$course数据 $majors = array(); foreach($course as $item){ extract($item); $key = "{$school_name}:{$course_name}"; if(!isset($majors[$key]){ $majors[$key] = array( "course_name" => $course_name, "school_name" => $school_name, "major_name" => array($major_name) ); }else{ // 已存在key记录且major_name未重复, 追加 marjor_name if(!in_array($marjor_name, $majors[$key]['major_name']){ $majors[$key]['major_name'][] = $marjor_name; } } } // 提取数组 $majors = array_values($majors); print_r($majors); // 这就是你要的 </code>
<code>$result = array(); foreach ($course as $val) { if(!isset($result[$val['school_name'].$val['course_name']]['major_name'])) { $result[$val['school_name'].$val['course_name']]['major_name'] = array(); $result[$val['school_name'].$val['course_name']]['course_name']=$val['course_name']; $result[$val['school_name'].$val['course_name']]['school_name']=$val['school_name']; } array_push($result[$val['school_name'].$val['course_name']]['major_name'],$val['major_name']); } var_dump(array_values($result)); // 输出结果如下 array(2) { [0]=> array(3) { ["major_name"]=> array(3) { [0]=> string(15) "计算机专业" [1]=> string(12) "物理专业" [2]=> string(12) "日语专业" } ["course_name"]=> string(12) "高等数学" ["school_name"]=> string(10) "某A高校" } [1]=> array(3) { ["major_name"]=> array(1) { [0]=> string(15) "计算机专业" } ["course_name"]=> string(15) "马克思概论" ["school_name"]=> string(10) "某B高校" } }</code>