Usually, we use SQL to extract some columns from the database that meet the requirements, and then we use the program to generate three-dimensional data with columns.
Here is the data from the database.
1 /* array */
2 $arr = array(
3 0=>array('id'=>1, 'classid'=>1, 'classtitle'=>'Government News', 'title'=>'The 9th National Games Opened Official Microblog', 'content'=>'test'),
4 1=>array('id'=>2, 'classid'=>1, 'classtitle'=>'Government News', 'title'=>'Where you have to go in summer', 'content'=>'test'),
5 2=>array('id'=>3, 'classid'=>2, 'classtitle'=>'IT Journalism', 'title'=>'Guiyang Fei and Thailand only need 2 580 yuan', 'content'=>'test'),
6 3=>array('id'=>4, 'classid'=>3, 'classtitle'=>'news on current events', 'title'=>'Wherever you go this summer, you will drift evenly.', 'content'=>'test'),
7 4=>array('id'=>5, 'classid'=>1, 'classtitle'=>'Government News', 'title'=>'Selection of "Meeting Site" Modeling for Zunyi Airport', 'content'=>'test'),
8 5=>array('id'=>6, 'classid'=>2, 'classtitle'=>'IT Journalism', 'title'=>'July Libo praised the aristocrats in the mountains and rivers', 'content'=>'test'),
9 6=>array('id'=>7, 'classid'=>3, 'classtitle'=>'news on current events', 'title'=>'Get rid of air conditioning and enjoy 20℃Midsummer', 'content'=>'test'),
10 7=>array('id'=>8, 'classid'=>3, 'classtitle'=>'news on current events', 'title'=>'"June 6" Buyi Song Festival in Libo is rich and colorful', 'content'=>'test'),
11 8=>array('id'=>9, 'classid'=>1, 'classtitle'=>'Government News', 'title'=>'Looking for the homeland of the world's miniskirts', 'content'=>'test'),
12 9=>array('id'=>10, 'classid'=>4, 'classtitle'=>'Regional News', 'title'=>'"Yi Folk Songs Selected into the List of Non-material Cultural Heritage', 'content'=>'test'),
13 10=>array('id'=>11, 'classid'=>4, 'classtitle'=>'Regional News', 'title'=>'Bangaoshui Village is as beautiful as Phoenix Feather', 'content'=>'test'),
14 11=>array('id'=>12, 'classid'=>4, 'classtitle'=>'Regional News', 'title'=>'There is a stage called Wetland Park in Guiyang.', 'content'=>'test'),
15 12=>array('id'=>13, 'classid'=>1, 'classtitle'=>'Government News', 'title'=>'Zhaodi Flows Brilliant to Welcome Lotus Festival', 'content'=>'test')
16 );
Convert to the following format: (categorized by columns)
1 Array
2 (
3 [1] => Array
4 (
5 [title]=> Government News
6 [1] => Array
7 (
8 [id] => 1
9 [title]=> The 9th National Games Opened Official Microblog
10 [content]=> On the morning of July 12, 2011, the official micro-blog of the 9th National Minority Traditional Sports Games was launched and signed in cooperation with Tencent.com
11 )
12
13 [2] => Array
14 (
15 [id] => 2
16 [title] = Where you have to go in summer
17 [content]=> Speaking of Guiyang, the word "Lincheng" must be reflected in my mind. There is a light mist in the green mountains and clear water.
18 )
19
20 [5] => Array
21 (
22 [id] => 5
23 [title]=> Zunyi Airport Selected "Meeting Site" Modeling
24 [content]=> Libo is a gathering place of ethnic minorities mainly composed of Buyi, Shui, Miao and Yao nationalities.
25 )
26
27 [9] => Array
28 (
29 [id] => 9
30 [title]=> Looking for the homeland of the world's miniskirts
31 [content]=> Speaking of Guiyang, the word "Lincheng" must come to mind.
32 )
33
34 [13] => Array
35 (
36 [id] => 13
37 [title]=> Zhaodi Brilliant Welcome Lotus Festival
38 [content]=> Speaking of Guiyang, the word "Lincheng" must come to mind.
39 )
40
41 )
42
43 [2] => Array
44 (
45 [title]=> IT News
46 [3] => Array
47 (
48 [id] => 3
49 [title]=> Guiyang Fei Thailand only costs 2 580 yuan
50 [content] => test
51 )
52
53 [6] => Array
54 (
55 [id] => 6
56 [title]=> July Libo's praise and admiration of the nobles in the mountains and waters
57 [content] => test
58 )
59
60 )
61
62 [3] => Array
63 (
64 [title]=> Current News
65 [4] => Array
66 (
67 [id] => 4
68 [title]=> Everywhere you go this summer, you will drift evenly.
69 [content] => test
70 )
71
72 [7] => Array
73 (
74 [id] => 7
75 [title]=> Get rid of air conditioning and enjoy the midsummer of 20 degrees Celsius
76 [content] => test
77 )
78
79 [8] => Array
80 (
81 [id] => 8
82 [title]=> Libo "June 6" Buyi Song Festival is rich and colorful
83 [content] => test
84 )
85
86 )
87
88 [4] => Array
89 (
90 [title]=> Regional News
91 [10] => Array
92 (
93 [id] => 10
94 [title]=> "Yi Folk Song" is included in the list of intangible cultural heritage
95 [content] => test
96 )
97
98 [11] => Array
99 (
100 [id] => 11
101 [title]=> Bangaoshui Village is as beautiful as Phoenix feathers
102 [content] => test
103 )
104
105 [12] => Array
106 (
107 [id] => 12
108 [title]=> There is a stage called Wetland Park in Guiyang.
109 [content] => test
110 )
111
112 )
113
114 )
Look, you may find it a little difficult, but... It takes only a few lines of code to OK (which is good as a written test). You can check each other's understanding of PHP arrays, PHP arrays can be very important.
The answer is as follows:
1 $array = Array();
2 foreach( $arr as $key=>$value ) {
3 $array[$value['classid']]['title'] = $value['classtitle'];
4 $array[$value['classid']][$value['id']]['id'] = $value['id'];
5 $array[$value['classid']][$value['id']]['title'] = $value['title'];
6 $array[$value['classid']][$value['id']]['content'] = $value['content'];
7 }
8 print_r($array);
Reproduced in: https://my.oschina.net/tiwer/blog/199854