PHP merge a two-dimensional array

Source: Internet
Author: User
1. this problem is mainly caused by the merging of two-dimensional arrays of P in the PH operation; 2. the initial array is as follows: {code ...} 3. the following array is expected: {code ...} 4. I hope I can get a reply to my first question. The younger brother was puzzled by this problem (no good answer was found, so reached out once) (in a hurry) 1. this problem is mainly caused by the merge of two-dimensional arrays of PH operation P;

2. the initial array is as follows:

$ Course = array ('course _ name' => 'higher Mathematics ', 'major _ name' => 'computer professional ', 'school _ name' => 'A University '), array ('course _ name' => 'higher Mathematics', 'major _ name' => 'physical specialize ', 'school _ name' => 'A University '), array ('course _ name' => 'higher Mathematics', 'major _ name' => 'Japanese major ', 'school _ name' => 'A University '), array ('course _ name' => 'Introduction to Marx', 'major _ name' => 'computer professional ', 'school _ name' => 'University of B '))

3. expected to get the following array:

$ Course = array ('course _ name' => 'advanced Mathematics ', 'major _ name' => array ('computer specialization', 'physical specialization ', 'Japanese major '), 'School _ name' => 'University A'), array ('course _ name' => 'Introduction to Marx ', 'Major _ name' => array ('computer major '), 'School _ name' =>' B University '))

4. I hope I can get a reply to my first question. The younger brother was confused by this question (no good answer was found, so reached out once) (in a hurry)

Reply content:

1. this problem is mainly caused by the merge of two-dimensional arrays of P operated by PH;

2. the initial array is as follows:

$ Course = array ('course _ name' => 'higher Mathematics ', 'major _ name' => 'computer professional ', 'school _ name' => 'A University '), array ('course _ name' => 'higher Mathematics', 'major _ name' => 'physical specialize ', 'school _ name' => 'A University '), array ('course _ name' => 'higher Mathematics', 'major _ name' => 'Japanese major ', 'school _ name' => 'A University '), array ('course _ name' => 'Introduction to Marx', 'major _ name' => 'computer professional ', 'school _ name' => 'University of B '))

3. expected to get the following array:

$ Course = array ('course _ name' => 'advanced Mathematics ', 'major _ name' => array ('computer specialization', 'physical specialization ', 'Japanese major '), 'School _ name' => 'University A'), array ('course _ name' => 'Introduction to Marx ', 'Major _ name' => array ('computer major '), 'School _ name' =>' B University '))

4. I hope I can get a reply to my first question. The younger brother was confused by this question (no good answer was found, so reached out once) (in a hurry)

First, the dimension of a piece of data is as follows:
"_ Course": [major 1, [major 2, major 3]
Therefore, we need to take the university course as the only difference

// Upload your $ course data $ 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 record exists and major_name is not repeated. append marjor_name if (! In_array ($ marjor_name, $ majors [$ key] ['major _ name']) {$ majors [$ key] ['major _ name'] [] = $ marjor_name ;}}// extract an array $ majors = array_values ($ majors ); print_r ($ majors); // This is what you want

$ 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 )); // the output result is as follows: array (2) {[0] => array (3) {["major_name"] => array (3) {[0] => string (15) "computer major" [1] => string (12) "Physical major" [2] => string (12) "Japanese major"} ["course_name"] => string (12) "advanced mathematics" ["school_name"] => string (10) "A university"} [1] => array (3) {["major_name"] => array (1) {[0] => string (15) "Computer major"} ["course_name"] => string (15) "Introduction to Marx" ["school_name"] => string (10) "a B University "}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.