Restructured array-PHP restructured two-dimensional array

Source: Internet
Author: User
How can we recompose the original two-dimensional array in the following form? The format of the original array {code...} after the reorganization is: {code...} How to recombine the original two-dimensional array in the following form?

Original Array

Array ([0] => Array ([section 1-2] => [section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ S1-205 [Section 5-6] => Linux advanced applications _ weekly _ XXXX_01-15 weeks _ A-910 [Section 7-8] => network security _ every two weeks _ XXXX_01-15 weeks _ A-507 [Section 9-10] =>) [1] => Array ([section 1-2] => IPV6 technology _ weekly _ XXXX_01-14 weeks _ S1-303 [section 3-4] => [Section 5-6] => network security _ weekly _ XXXX_01-15 weeks _ C-408 [Section 7-8] => [Section 9-10] =>) [2] => Array ([section 1-2] => network marketing _ weekly _ XXXX_01-14 weeks _ C-201 [section 3-4] => [Section 5-6] => Employment Guidance _ weekly _ XXXX_11-12 weeks _ B-202 [Section 7-8] => [Section 9-10] =>) [3] => Array ([section 1-2] => Linux advanced applications _ every week _ XXXX_01-15 weeks _ A-910/Web site development and maintenance _ every week _ Zhang Jinbo _ weeks _ A-910 [section 3-4] => [Section 5-6] => Cisco advanced applications _ weekly _ XXXX_01-14 weeks _ S1-305 [Section 7-8] => Linux advanced applications _ weekly _ XXXX_01-15 weekly _ S1-303 [Section 9-10] =>) [4] => Array ([section 1-2] => network security _ weekly _ XXXX_01-15 weeks _ A-508 [section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ A-908 [Section 5-6] => [Section 7-8] => [Section 9-10] => ))

The format after reorganization is:

Array ([0] => Array ([section 1-2] => [section 1-2] => IPV6 technology _ weekly _ XXXX_01-14 weeks _ S1-303 [section 1-2] => Network marketing _ weekly _ XXXX_01-14 weeks _ C-201 [section 1-2] => Linux advanced applications _ every week _ XXXX_01-15 weeks _ A-910/Web site development and maintenance _ every two weeks _ XXXX_01-15 weeks _ A-910 [section 1-2] => cybersecurity _ weekly _ XXXX_01-15 _ A-508) [1] => Array ([section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ S1-205 [section 3-4] => [section 3-4] => [section 3-4] => [section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ A-908) [2] => Array ([Section 5-6] => Linux advanced applications _ weekly _ XXXX_01-15 weeks _ A-910 [Section 5-6] => network security _ weekly _ XXXX_01-15 weeks _ C-408 [section 5-6] => Employment Guidance _ weekly _ XXXX_11-12 weeks _ B-202 [Section 5-6] => Cisco advanced applications _ weekly _ XXXX_01-14 weeks _ S1-305 [Section 5-6] =>) [3] => Array ([Section 7-8] => cybersecurity _ every two weeks _ XXXX_01-15 weeks _ A-507 [Section 7-8] => [Section 7-8] => [Section 7-8 7-8 section] => Linux advanced applications _ weekly _ XXXX_01-15 weeks _ S1-303 [Section 7-8] =>) [4] => Array ([Section 9-10] => [Section 9-10] => [Section 9-10] => [Section 9-10] => [Section 9-10] => ))

Reply content:

How can we recompose the original two-dimensional array in the following form?

Original Array

Array ([0] => Array ([section 1-2] => [section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ S1-205 [Section 5-6] => Linux advanced applications _ weekly _ XXXX_01-15 weeks _ A-910 [Section 7-8] => network security _ every two weeks _ XXXX_01-15 weeks _ A-507 [Section 9-10] =>) [1] => Array ([section 1-2] => IPV6 technology _ weekly _ XXXX_01-14 weeks _ S1-303 [section 3-4] => [Section 5-6] => network security _ weekly _ XXXX_01-15 weeks _ C-408 [Section 7-8] => [Section 9-10] =>) [2] => Array ([section 1-2] => network marketing _ weekly _ XXXX_01-14 weeks _ C-201 [section 3-4] => [Section 5-6] => Employment Guidance _ weekly _ XXXX_11-12 weeks _ B-202 [Section 7-8] => [Section 9-10] =>) [3] => Array ([section 1-2] => Linux advanced applications _ every week _ XXXX_01-15 weeks _ A-910/Web site development and maintenance _ every week _ Zhang Jinbo _ weeks _ A-910 [section 3-4] => [Section 5-6] => Cisco advanced applications _ weekly _ XXXX_01-14 weeks _ S1-305 [Section 7-8] => Linux advanced applications _ weekly _ XXXX_01-15 weekly _ S1-303 [Section 9-10] =>) [4] => Array ([section 1-2] => network security _ weekly _ XXXX_01-15 weeks _ A-508 [section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ A-908 [Section 5-6] => [Section 7-8] => [Section 9-10] => ))

The format after reorganization is:

Array ([0] => Array ([section 1-2] => [section 1-2] => IPV6 technology _ weekly _ XXXX_01-14 weeks _ S1-303 [section 1-2] => Network marketing _ weekly _ XXXX_01-14 weeks _ C-201 [section 1-2] => Linux advanced applications _ every week _ XXXX_01-15 weeks _ A-910/Web site development and maintenance _ every two weeks _ XXXX_01-15 weeks _ A-910 [section 1-2] => cybersecurity _ weekly _ XXXX_01-15 _ A-508) [1] => Array ([section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ S1-205 [section 3-4] => [section 3-4] => [section 3-4] => [section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ A-908) [2] => Array ([Section 5-6] => Linux advanced applications _ weekly _ XXXX_01-15 weeks _ A-910 [Section 5-6] => network security _ weekly _ XXXX_01-15 weeks _ C-408 [section 5-6] => Employment Guidance _ weekly _ XXXX_11-12 weeks _ B-202 [Section 5-6] => Cisco advanced applications _ weekly _ XXXX_01-14 weeks _ S1-305 [Section 5-6] =>) [3] => Array ([Section 7-8] => cybersecurity _ every two weeks _ XXXX_01-15 weeks _ A-507 [Section 7-8] => [Section 7-8] => [Section 7-8 7-8 section] => Linux advanced applications _ weekly _ XXXX_01-15 weeks _ S1-303 [Section 7-8] =>) [4] => Array ([Section 9-10] => [Section 9-10] => [Section 9-10] => [Section 9-10] => [Section 9-10] => ))

[1] => Array ([section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ S1-205 [section 3-4] => [section 3-4] => [section 3-4] => [section 3-4] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ A-908)

If this key is the same, the subsequent key will overwrite the previous one. You should expect this result:

[Section 1-2] => Array ([0] => [1] => IPV6 technology _ weekly _ XXXX_01-14 weeks _ S1-303 [2] => network marketing _ weekly _ XXXX_01-14 weeks _ C-201 [3] => Linux advanced applications _ every week _ XXXX_01-15 weeks _ A-910/Web site development and maintenance _ every two weeks _ XXXX_01-15 weeks _ A-910 [4] => network security _ weekly _ XXXX_01-15 weeks _ A-508) [section 3-4] => Array ([0] => Web site development and maintenance _ weekly _ XXXX_01-15 weeks _ S1-205 [1] => [2] => [3] => [4] => Web site development and maintenance _ weekly _ XXXX_01-15 _ A-908)

You can write a function:

    public function listData($arr){        if(empty($arr)) return array();        $listData = array();        foreach($arr as $key=>$value){            foreach($value as $k=>$v){                $listData[$k][] = $v;            }        }        return $listData;    }

Test Array

$arr  = array(    0=>array(        'a'=>1,        'b'=>2,        'c'=>3,    ),    1=>array(        'a'=>11,        'b'=>22,        'c'=>33,    ),    2=>array(        'a'=>111,        'b'=>222,        'c'=>333,    ),);

Get:

array  'a' =>     array      0 => int 1      1 => int 11      2 => int 111  'b' =>     array      0 => int 2      1 => int 22      2 => int 222  'c' =>     array      0 => int 3      1 => int 33      2 => int 333

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.