Outline
花了2天的时间试图让这个工作…
<解决方案/strong>
我想用以下结构将我的数组重构为多维数组:
Level 1 -> All level 1s
Level 2 -> All level 2s
Level 3 -> Each level Three Item
Level 3 -> Each level Three Item
Level 2 -> All level 2s
Level 3 -> Each level Three Item
Level 3 -> Each level Three Item
Level 1 -> All level 1s
Level 2 -> All level 2s
Level 3 -> Each level Three Item
Level 3 -> Each level Three Item
Level 2 -> All level 2s
Level 3 -> Each level Three Item
Level 3 -> Each level Three Item
数据strong>
数据按我需要的顺序写入。例如,所有直接位于Level 1
下方的level 2
应该位于其母Level 1
内,直到下一个Level 1
出现在数据中。这对于Level 2
和3
应该是相同的。基本上,直到出现一个更高的数字,它应该开始一个新的行。
我有很多不同的值,下面的数据只是一个例子。在数组内部,level
指的是它应该位于哪个级别。
array (
0 =>
array (
'key' => 0,
'id' => '7',
'level' => '1',
'name' => 'About Me',
),
1 =>
array (
'key' => 1,
'id' => '78',
'level' => '2',
'name' => 'My Conduct at Work',
),
2 =>
array (
'key' => 2,
'id' => '270',
'level' => '3',
'name' => 'Dispute Resolution/Grievance',
),
3 =>
array (
'key' => 3,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
4 =>
array (
'key' => 4,
'id' => '77',
'level' => '2',
'name' => 'My Environment, Health & Safety',
),
5 =>
array (
'key' => 5,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
6 =>
array (
'key' => 6,
'id' => '238',
'level' => '3',
'name' => 'Policies and Guidance',
),
7 =>
array (
'key' => 7,
'id' => '79',
'level' => '2',
'name' => 'My GSK Employment Journey',
),
8 =>
array (
'key' => 8,
'id' => '274',
'level' => '3',
'name' => 'Changes in Role',
),
9 =>
array (
'key' => 9,
'id' => '275',
'level' => '3',
'name' => 'Leaving GSK',
),
10 =>
array (
'key' => 10,
'id' => '273',
'level' => '3',
'name' => 'My Moves',
),
11 =>
array (
'key' => 11,
'id' => '272',
'level' => '3',
'name' => 'Starting at GSK',
),
12 =>
array (
'key' => 12,
'id' => '80',
'level' => '2',
'name' => 'My Personal Information',
),
13 =>
array (
'key' => 13,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
14 =>
array (
'key' => 14,
'id' => '276',
'level' => '3',
'name' => 'Manage my Personal Information',
),
15 =>
array (
'key' => 15,
'id' => '76',
'level' => '2',
'name' => 'My Talent, Performance & Development',
),
16 =>
array (
'key' => 16,
'id' => '266',
'level' => '3',
'name' => 'My Development',
),
17 =>
array (
'key' => 17,
'id' => '268',
'level' => '3',
'name' => 'My Learning',
),
18 =>
array (
'key' => 18,
'id' => '267',
'level' => '3',
'name' => 'My Performance',
),
19 =>
array (
'key' => 19,
'id' => '75',
'level' => '2',
'name' => 'My TotalReward',
),
20 =>
array (
'key' => 20,
'id' => '287',
'level' => '3',
'name' => 'Learn More My Benefits',
),
21 =>
array (
'key' => 21,
'id' => '288',
'level' => '3',
'name' => 'Learn More My Pay',
),
22 =>
array (
'key' => 22,
'id' => '264',
'level' => '3',
'name' => 'My Benefits',
),
23 =>
array (
'key' => 23,
'id' => '263',
'level' => '3',
'name' => 'My Pay',
),
24 =>
array (
'key' => 24,
'id' => '265',
'level' => '3',
'name' => 'My Recognition',
),
25 =>
array (
'key' => 25,
'id' => '81',
'level' => '2',
'name' => 'Time Off, Time & Attendance',
),
26 =>
array (
'key' => 26,
'id' => '281',
'level' => '3',
'name' => 'Flexible Working',
),
27 =>
array (
'key' => 27,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
28 =>
array (
'key' => 28,
'id' => '279',
'level' => '3',
'name' => 'Leave of Absence',
),
29 =>
array (
'key' => 29,
'id' => '280',
'level' => '3',
'name' => 'Time & Attendance',
),
30 =>
array (
'key' => 30,
'id' => '278',
'level' => '3',
'name' => 'Time Off',
),
31 =>
array (
'key' => 31,
'id' => '1',
'level' => '1',
'name' => 'Buying and Paying',
),
32 =>
array (
'key' => 32,
'id' => '1',
'level' => '2',
'name' => 'Budget Management',
),
33 =>
array (
'key' => 33,
'id' => '1',
'level' => '3',
'name' => 'Asset scrapping or sale (equipment, PC …)',
),
34 =>
array (
'key' => 34,
'id' => '221',
'level' => '3',
'name' => 'Cost center creation & change',
),
35 =>
array (
'key' => 35,
'id' => '2',
'level' => '3',
'name' => 'Finance Partner',
),
36 =>
array (
'key' => 36,
'id' => '3',
'level' => '3',
'name' => 'Operating Budget',
),
37 =>
array (
'key' => 37,
'id' => '222',
'level' => '3',
'name' => 'Provision request',
),
38 =>
array (
'key' => 38,
'id' => '4',
'level' => '3',
'name' => 'X-charge (Cost Transfer)',
),
39 =>
array (
'key' => 39,
'id' => '3',
'level' => '2',
'name' => 'Checks, GOA & X-Charge',
),
40 =>
array (
'key' => 40,
'id' => '10',
'level' => '3',
'name' => 'Check Requests',
),
41 =>
array (
'key' => 41,
'id' => '11',
'level' => '3',
'name' => 'Collections, credits and debts',
),
42 =>
array (
'key' => 42,
'id' => '12',
'level' => '3',
'name' => 'Discounts, Rebates, Commissions and Allowances',
),
43 =>
array (
'key' => 43,
'id' => '217',
'level' => '3',
'name' => 'Grant of Authority',
),
44 =>
array (
'key' => 44,
'id' => '4',
'level' => '3',
'name' => 'X-charge (Cost Transfer)',
),
45 =>
array (
'key' => 45,
'id' => '6',
'level' => '2',
'name' => 'Finance for Finance',
),
46 =>
array (
'key' => 46,
'id' => '241',
'level' => '3',
'name' => 'Finance Assurance',
),
47 =>
array (
'key' => 47,
'id' => '22',
'level' => '3',
'name' => 'Order to Cash',
),
48 =>
array (
'key' => 48,
'id' => '23',
'level' => '3',
'name' => 'R2R - Group Consolidation and Intercompany Profit',
),
49 =>
array (
'key' => 49,
'id' => '24',
'level' => '3',
'name' => 'R2R - Management Reporting',
),
50 =>
array (
'key' => 50,
'id' => '25',
'level' => '3',
'name' => 'R2R - Record, Forms & Reports',
),
51 =>
array (
'key' => 51,
'id' => '251',
'level' => '3',
'name' => 'R2R - Record, General Information',
),
52 =>
array (
'key' => 52,
'id' => '252',
'level' => '3',
'name' => 'R2R - Record, Get Help',
),
53 =>
array (
'key' => 53,
'id' => '26',
'level' => '3',
'name' => 'R2R - Statutory Reporting',
),
54 =>
array (
'key' => 54,
'id' => '27',
'level' => '3',
'name' => 'R2R - Technical Accounting',
),
55 =>
array (
'key' => 55,
'id' => '28',
'level' => '3',
'name' => 'Supply Chain Accounting',
),
56 =>
array (
'key' => 56,
'id' => '65',
'level' => '2',
'name' => 'Invoice and Supplier Services',
),
57 =>
array (
'key' => 57,
'id' => '13',
'level' => '3',
'name' => 'Invoice and Supplier Services',
),
58 =>
array (
'key' => 58,
'id' => '93',
'level' => '2',
'name' => 'Mergers, Acquisitions and Divestments Support',
),
59 =>
array (
'key' => 59,
'id' => '317',
'level' => '3',
'name' => 'Engage with MAD',
),
60 =>
array (
'key' => 60,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
61 =>
array (
'key' => 61,
'id' => '66',
'level' => '2',
'name' => 'Procurement Card',
),
62 =>
array (
'key' => 62,
'id' => '14',
'level' => '3',
'name' => 'Procurement Card Program',
),
63 =>
array (
'key' => 63,
'id' => '10',
'level' => '2',
'name' => 'Procurement for Procurement',
),
64 =>
array (
'key' => 64,
'id' => '49',
'level' => '3',
'name' => 'Contract Management',
),
65 =>
array (
'key' => 65,
'id' => '50',
'level' => '3',
'name' => 'Market Intelligence/Research Portal',
),
66 =>
array (
'key' => 66,
'id' => '325',
'level' => '3',
'name' => 'Procurement Training',
),
67 =>
array (
'key' => 67,
'id' => '51',
'level' => '3',
'name' => 'Risk Management',
),
68 =>
array (
'key' => 68,
'id' => '52',
'level' => '3',
'name' => 'Spend Analytics',
),
69 =>
array (
'key' => 69,
'id' => '53',
'level' => '3',
'name' => 'Supplier Diversity & Development Support',
),
70 =>
array (
'key' => 70,
'id' => '54',
'level' => '3',
'name' => 'Supplier Relationship & Performance Management',
),
71 =>
array (
'key' => 71,
'id' => '64',
'level' => '2',
'name' => 'Purchases',
),
72 =>
array (
'key' => 72,
'id' => '9',
'level' => '3',
'name' => 'Buy Something',
),
73 =>
array (
'key' => 73,
'id' => '259',
'level' => '3',
'name' => 'Learn About',
),
74 =>
array (
'key' => 74,
'id' => '3',
'level' => '1',
'name' => 'Expenses & Travel',
),
75 =>
array (
'key' => 75,
'id' => '69',
'level' => '2',
'name' => 'Book a Flight',
),
76 =>
array (
'key' => 76,
'id' => '72',
'level' => '3',
'name' => 'Airline Reservations',
),
77 =>
array (
'key' => 77,
'id' => '71',
'level' => '2',
'name' => 'Book a Ground Transportaion',
),
78 =>
array (
'key' => 78,
'id' => '319',
'level' => '3',
'name' => 'Rail',
),
79 =>
array (
'key' => 79,
'id' => '76',
'level' => '3',
'name' => 'Taxi, Chauffeur & Limousine Services',
),
80 =>
array (
'key' => 80,
'id' => '70',
'level' => '2',
'name' => 'Book a Hotel',
),
81 =>
array (
'key' => 81,
'id' => '75',
'level' => '3',
'name' => 'Hotel Room Booking',
),
82 =>
array (
'key' => 82,
'id' => '67',
'level' => '2',
'name' => 'Credit Card',
),
83 =>
array (
'key' => 83,
'id' => '68',
'level' => '3',
'name' => 'Amex',
),
84 =>
array (
'key' => 84,
'id' => '13',
'level' => '2',
'name' => 'Expense Policies and Information',
),
85 =>
array (
'key' => 85,
'id' => '69',
'level' => '3',
'name' => 'Expense Policies',
),
86 =>
array (
'key' => 86,
'id' => '71',
'level' => '3',
'name' => 'VAT Reclaim',
),
87 =>
array (
'key' => 87,
'id' => '68',
'level' => '2',
'name' => 'Report Expenses',
),
88 =>
array (
'key' => 88,
'id' => '70',
'level' => '3',
'name' => 'Report Expenses',
),
89 =>
array (
'key' => 89,
'id' => '15',
'level' => '2',
'name' => 'Travel Information',
),
90 =>
array (
'key' => 90,
'id' => '73',
'level' => '3',
'name' => 'Benefits Administration and Management (UK/US)',
),
91 =>
array (
'key' => 91,
'id' => '74',
'level' => '3',
'name' => 'Commuter Services/Shuttle Busses',
),
92 =>
array (
'key' => 92,
'id' => '77',
'level' => '3',
'name' => 'Health while travelling',
),
93 =>
array (
'key' => 93,
'id' => '320',
'level' => '3',
'name' => 'Travel Alerts',
),
94 =>
array (
'key' => 94,
'id' => '14',
'level' => '2',
'name' => 'Travel Insurances',
),
95 =>
array (
'key' => 95,
'id' => '65',
'level' => '3',
'name' => 'Loss and theft',
),
96 =>
array (
'key' => 96,
'id' => '66',
'level' => '3',
'name' => 'Material damages insurance',
),
97 =>
array (
'key' => 97,
'id' => '67',
'level' => '3',
'name' => 'Travel insurance for personal injury',
),
98 =>
array (
'key' => 98,
'id' => '4',
'level' => '1',
'name' => 'IT Services & Access',
),
99 =>
array (
'key' => 99,
'id' => '16',
'level' => '2',
'name' => 'Basic IT Access',
),
100 =>
array (
'key' => 100,
'id' => '78',
'level' => '3',
'name' => 'Getting an ID',
),
101 =>
array (
'key' => 101,
'id' => '254',
'level' => '3',
'name' => 'Remote Access Tokens',
),
102 =>
array (
'key' => 102,
'id' => '79',
'level' => '3',
'name' => 'Request Access',
),
103 =>
array (
'key' => 103,
'id' => '255',
'level' => '3',
'name' => 'Request Access - Unix, Linux, VAX, AS400',
),
104 =>
array (
'key' => 104,
'id' => '23',
'level' => '2',
'name' => 'Business Unit Systems',
),
105 =>
array (
'key' => 105,
'id' => '93',
'level' => '3',
'name' => 'Corporate Finance Systems',
),
106 =>
array (
'key' => 106,
'id' => '84',
'level' => '3',
'name' => 'Customer Relationship Management & Marketing Systems',
),
107 =>
array (
'key' => 107,
'id' => '120',
'level' => '3',
'name' => 'Discovery Systems',
),
108 =>
array (
'key' => 108,
'id' => '94',
'level' => '3',
'name' => 'Global Finance Systems',
),
109 =>
array (
'key' => 109,
'id' => '125',
'level' => '3',
'name' => 'HH and EHS Systems',
),
110 =>
array (
'key' => 110,
'id' => '118',
'level' => '3',
'name' => 'IT support for audits and inspections',
),
111 =>
array (
'key' => 111,
'id' => '127',
'level' => '3',
'name' => 'Legal & Procurement Systems',
),
112 =>
array (
'key' => 112,
'id' => '95',
'level' => '3',
'name' => 'Make MRP Services Systems',
),
113 =>
array (
'key' => 113,
'id' => '85',
'level' => '3',
'name' => 'Market and Sell systems',
),
114 =>
array (
'key' => 114,
'id' => '121',
'level' => '3',
'name' => 'Medical Systems',
),
115 =>
array (
'key' => 115,
'id' => '96',
'level' => '3',
'name' => 'Pack & Move Services Systems',
),
116 =>
array (
'key' => 116,
'id' => '86',
'level' => '3',
'name' => 'PPV & Sales Reporting & Compliance Systems',
),
117 =>
array (
'key' => 117,
'id' => '122',
'level' => '3',
'name' => 'Pre-Clinical Systems',
),
118 =>
array (
'key' => 118,
'id' => '119',
'level' => '3',
'name' => 'Quality, Risk & Analytics Systems',
),
119 =>
array (
'key' => 119,
'id' => '312',
'level' => '3',
'name' => 'SAP Commercial',
),
120 =>
array (
'key' => 120,
'id' => '128',
'level' => '3',
'name' => 'SAP Manufacturing',
),
121 =>
array (
'key' => 121,
'id' => '129',
'level' => '3',
'name' => 'Supply Chain Planning Services Systems',
),
122 =>
array (
'key' => 122,
'id' => '123',
'level' => '3',
'name' => 'Test Lab Systems & Support',
),
123 =>
array (
'key' => 123,
'id' => '27',
'level' => '2',
'name' => 'Desk Phones, Smartphones & Tablets',
),
124 =>
array (
'key' => 124,
'id' => '110',
'level' => '3',
'name' => 'Desk Phone',
),
125 =>
array (
'key' => 125,
'id' => '111',
'level' => '3',
'name' => 'Fax',
),
126 =>
array (
'key' => 126,
'id' => '247',
'level' => '3',
'name' => 'IP Phone Communicator Configuration',
),
127 =>
array (
'key' => 127,
'id' => '112',
'level' => '3',
'name' => 'IP Phone Communicator Help & How To',
),
128 =>
array (
'key' => 128,
'id' => '104',
'level' => '3',
'name' => 'Mobile and Smart phones',
),
129 =>
array (
'key' => 129,
'id' => '250',
'level' => '3',
'name' => 'Ordering an IP Phone and Headsets',
),
130 =>
array (
'key' => 130,
'id' => '107',
'level' => '3',
'name' => 'Synchronization with MS Outlook',
),
131 =>
array (
'key' => 131,
'id' => '108',
'level' => '3',
'name' => 'Tablets',
),
132 =>
array (
'key' => 132,
'id' => '113',
'level' => '3',
'name' => 'Teleconference solution choice',
),
133 =>
array (
'key' => 133,
'id' => '114',
'level' => '3',
'name' => 'Video phone',
),
134 =>
array (
'key' => 134,
'id' => '109',
'level' => '3',
'name' => 'Wifi Guest for Smartphone',
),
135 =>
array (
'key' => 135,
'id' => '18',
'level' => '2',
'name' => 'EMail and Collaboration',
),
136 =>
array (
'key' => 136,
'id' => '87',
'level' => '3',
'name' => 'Distribution lists',
),
137 =>
array (
'key' => 137,
'id' => '324',
'level' => '3',
'name' => 'Effective Collaboration',
),
138 =>
array (
'key' => 138,
'id' => '88',
'level' => '3',
'name' => 'Filtering spam',
),
139 =>
array (
'key' => 139,
'id' => '89',
'level' => '3',
'name' => 'Group mailbox',
),
140 =>
array (
'key' => 140,
'id' => '80',
'level' => '3',
'name' => 'Live Meeting',
),
141 =>
array (
'key' => 141,
'id' => '90',
'level' => '3',
'name' => 'Lotus Notes',
),
142 =>
array (
'key' => 142,
'id' => '81',
'level' => '3',
'name' => 'Office communicator',
),
143 =>
array (
'key' => 143,
'id' => '91',
'level' => '3',
'name' => 'Outlook Mailbox & calendar',
),
144 =>
array (
'key' => 144,
'id' => '92',
'level' => '3',
'name' => 'Securing email',
),
145 =>
array (
'key' => 145,
'id' => '83',
'level' => '3',
'name' => 'Videoconference',
),
146 =>
array (
'key' => 146,
'id' => '19',
'level' => '2',
'name' => 'Hardware',
),
147 =>
array (
'key' => 147,
'id' => '97',
'level' => '3',
'name' => 'Borrow hardware',
),
148 =>
array (
'key' => 148,
'id' => '98',
'level' => '3',
'name' => 'Buy Hardware',
),
149 =>
array (
'key' => 149,
'id' => '99',
'level' => '3',
'name' => 'Move hardware',
),
150 =>
array (
'key' => 150,
'id' => '100',
'level' => '3',
'name' => 'Protect computer',
),
151 =>
array (
'key' => 151,
'id' => '101',
'level' => '3',
'name' => 'Repair hardware',
),
152 =>
array (
'key' => 152,
'id' => '102',
'level' => '3',
'name' => 'Smartphone and Mobile Devices',
),
153 =>
array (
'key' => 153,
'id' => '103',
'level' => '3',
'name' => 'Upgrade computer',
),
154 =>
array (
'key' => 154,
'id' => '8',
'level' => '2',
'name' => 'IT for IT',
),
155 =>
array (
'key' => 155,
'id' => '35',
'level' => '3',
'name' => 'Application Infrastructure Provisioning',
),
156 =>
array (
'key' => 156,
'id' => '216',
'level' => '3',
'name' => 'Application, Process and Data Integration',
),
157 =>
array (
'key' => 157,
'id' => '42',
'level' => '3',
'name' => 'Connect to or from External Partners',
),
158 =>
array (
'key' => 158,
'id' => '36',
'level' => '3',
'name' => 'Data Center Operations and Facilities',
),
159 =>
array (
'key' => 159,
'id' => '37',
'level' => '3',
'name' => 'Database Services',
),
160 =>
array (
'key' => 160,
'id' => '47',
'level' => '3',
'name' => 'Hosting Services',
),
161 =>
array (
'key' => 161,
'id' => '321',
'level' => '3',
'name' => 'Infrastructure Services',
),
162 =>
array (
'key' => 162,
'id' => '39',
'level' => '3',
'name' => 'IT Audit and Risk',
),
163 =>
array (
'key' => 163,
'id' => '40',
'level' => '3',
'name' => 'Malicious Code Management',
),
164 =>
array (
'key' => 164,
'id' => '41',
'level' => '3',
'name' => 'Network Services',
),
165 =>
array (
'key' => 165,
'id' => '326',
'level' => '3',
'name' => 'Optimise Application Performance',
),
166 =>
array (
'key' => 166,
'id' => '28',
'level' => '2',
'name' => 'Printer, archiving & scanning',
),
167 =>
array (
'key' => 167,
'id' => '115',
'level' => '3',
'name' => 'Archiving',
),
168 =>
array (
'key' => 168,
'id' => '116',
'level' => '3',
'name' => 'Desk Printers',
),
169 =>
array (
'key' => 169,
'id' => '117',
'level' => '3',
'name' => 'Printing & scanning',
),
170 =>
array (
'key' => 170,
'id' => '74',
'level' => '2',
'name' => 'Remote Access Tokens',
),
171 =>
array (
'key' => 171,
'id' => '11',
'level' => '2',
'name' => 'Reporting and Data Management',
),
172 =>
array (
'key' => 172,
'id' => '55',
'level' => '3',
'name' => 'BI/DW (Business Intelligence/Data Warehouse)',
),
173 =>
array (
'key' => 173,
'id' => '242',
'level' => '3',
'name' => 'Business Objects Application',
),
174 =>
array (
'key' => 174,
'id' => '56',
'level' => '3',
'name' => 'CERPS Data Management Service (CDMO)',
),
175 =>
array (
'key' => 175,
'id' => '243',
'level' => '3',
'name' => 'Cognos Application',
),
176 =>
array (
'key' => 176,
'id' => '244',
'level' => '3',
'name' => 'Cognos Planning Application',
),
177 =>
array (
'key' => 177,
'id' => '245',
'level' => '3',
'name' => 'DataStage Application',
),
178 =>
array (
'key' => 178,
'id' => '246',
'level' => '3',
'name' => 'DataWarehouse Service',
),
179 =>
array (
'key' => 179,
'id' => '57',
'level' => '3',
'name' => 'ONE Service',
),
180 =>
array (
'key' => 180,
'id' => '29',
'level' => '2',
'name' => 'Software',
),
181 =>
array (
'key' => 181,
'id' => '313',
'level' => '3',
'name' => 'Manage AIT',
),
182 =>
array (
'key' => 182,
'id' => '124',
'level' => '3',
'name' => 'Requesting or Removing Software',
),
183 =>
array (
'key' => 183,
'id' => '30',
'level' => '2',
'name' => 'Wi-Fi & internet',
),
184 =>
array (
'key' => 184,
'id' => '131',
'level' => '3',
'name' => 'GSK network for business partners',
),
185 =>
array (
'key' => 185,
'id' => '132',
'level' => '3',
'name' => 'Wifi for visitors',
),
186 =>
array (
'key' => 186,
'id' => '133',
'level' => '3',
'name' => 'Wifi in the office',
),
187 =>
array (
'key' => 187,
'id' => '134',
'level' => '3',
'name' => 'Working from Home',
),
188 =>
array (
'key' => 188,
'id' => '135',
'level' => '3',
'name' => 'Working in the Office',
),
189 =>
array (
'key' => 189,
'id' => '5',
'level' => '1',
'name' => 'My Team',
),
190 =>
array (
'key' => 190,
'id' => '88',
'level' => '2',
'name' => 'Environment, Health & Safety',
),
191 =>
array (
'key' => 191,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
192 =>
array (
'key' => 192,
'id' => '89',
'level' => '2',
'name' => 'My Teams' Conduct at Work',
),
193 =>
array (
'key' => 193,
'id' => '296',
'level' => '3',
'name' => 'Code of Conduct',
),
194 =>
array (
'key' => 194,
'id' => '294',
'level' => '3',
'name' => 'Disciplinary',
),
195 =>
array (
'key' => 195,
'id' => '270',
'level' => '3',
'name' => 'Dispute Resolution/Grievance',
),
196 =>
array (
'key' => 196,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
197 =>
array (
'key' => 197,
'id' => '90',
'level' => '2',
'name' => 'My Teams' Employment Journey',
),
198 =>
array (
'key' => 198,
'id' => '301',
'level' => '3',
'name' => 'Changes',
),
199 =>
array (
'key' => 199,
'id' => '297',
'level' => '3',
'name' => 'Hiring',
),
200 =>
array (
'key' => 200,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
201 =>
array (
'key' => 201,
'id' => '275',
'level' => '3',
'name' => 'Leaving GSK',
),
202 =>
array (
'key' => 202,
'id' => '300',
'level' => '3',
'name' => 'Manage an International Assignment',
),
203 =>
array (
'key' => 203,
'id' => '141',
'level' => '3',
'name' => 'Onboarding',
),
204 =>
array (
'key' => 204,
'id' => '299',
'level' => '3',
'name' => 'Transfer/Promote/Change Job Permanent',
),
205 =>
array (
'key' => 205,
'id' => '298',
'level' => '3',
'name' => 'Transfer/Promote/Change Job Temporary',
),
206 =>
array (
'key' => 206,
'id' => '91',
'level' => '2',
'name' => 'Reporting , Data & Analytics',
),
207 =>
array (
'key' => 207,
'id' => 'TBC',
'level' => '3',
'name' => 'Learn More',
),
208 =>
array (
'key' => 208,
'id' => '304',
'level' => '3',
'name' => 'My Teams' Information',
),
209 =>
array (
'key' => 209,
'id' => '303',
'level' => '3',
'name' => 'Workday Reporting',
),
210 =>
array (
'key' => 210,
'id' => '87',
'level' => '2',
'name' => 'Talent, Performance & Development',
),
211 =>
array (
'key' => 211,
'id' => '290',
'level' => '3',
'name' => 'Learning',
),
212 =>
array (
'key' => 212,
'id' => '291',
'level' => '3',
'name' => 'Performance & Development',
),
213 =>
array (
'key' => 213,
'id' => '292',
'level' => '3',
'name' => 'Talent',
)
)
您可以尝试使用PHP链接操作符&,尝试如下:
$collection = array();
$list = yourArray[];
foreach ($list as $item) {
if (isset($list[$item['key']])) {
$list[$item['key']]['levels'][$item['key']] =& $list[$item['key']];
} else {
$collection[$item['key']] =& $list[$item['key']];
}
}
第二变体
$row = array();
if (is_array($source) && !empty($source)) {
foreach ($source as $item) {
if (!isset($row[$item['level']])) {
$row[$item['level']] = array($item['level']);
$row[$item['level']]['parent'] = $item['level'] - 1;
}
if(isset($row[$item['level']])){
$row[$item['level']]['child'][$item['key']] = $item;
}
}
if(!empty($row)){
foreach($row as $rowK => $rowV){
if(isset($row[$rowV['parent']])){
$row[$rowV['parent']]['level-' . $rowK][] =& $row[$rowK];
} else {
$collection[$rowK] =& $row[$rowK];
}
}
}
}
这是我想到的…这不是一个非常有效的方法,但也许有人可以把它变成一个奇特的递归函数,使它抽象——我已经尝试了一点,但我真的很不擅长递归:- p
function cascade_array ($array, $current_level = 1) {
// filter only the array elements with the level==current_level
$chunks = array_filter ($array, function ($v) use ($current_level) {
return $v['level'] == 1; }
);
// extract the keys from those elements an put them in reverse order
$splice_keys = array_reverse(array_keys($chunks));
// now splice the array at those keys and put the
// array-parts in a result-array, so you have an array of e.g. all level1
foreach ($splice_keys as $offset) {
$results[] = array_splice($array, $offset);
}
//now walk through the results and basically do the same
foreach($results as &$level) {
$level[0]['children'] = array();
$array = array_splice($level, 1);
$chunks = array_filter ($array, function ($v) use ($current_level) {
return $v['level'] == 2; }
);
$splice_keys = array_reverse(array_keys($chunks));
foreach($splice_keys as $offset) {
$level[0]['children'][] = array_splice($array, $offset);
}
foreach($level[0]['children'] as &$child_level) {
$child_level[0]['children'] = array();
$array = array_splice($child_level, 1);
$chunks = array_filter ($array, function ($v) use ($current_level) {
return $v['level'] == 3; }
);
$splice_keys = array_reverse(array_keys($chunks));
foreach($splice_keys as $offset) {
$child_level[0]['children'][] = array_splice($array, $offset);
}
}
}
return $results;
}
代码实际上是相当直接的,我想。下面是一个工作示例:http://codepad.viper-7.com/kb5JBc -我希望它有帮助:)
递归实体从来都不是一件容易的事,尤其是当你刚开始使用PHP时。
下面是一个简短的函数,它通过使用php引用来避免使用递归函数。引用可能令人困惑,递归也一样。最好的建议是仔细阅读并尝试一些方法,以便弄清楚它们是如何工作的。
下面的工作使用一个堆栈来跟踪级别,并不断地引用最后一层来找到最新的父级。
function stackItems( $items ){
$level = 0;
$return = array();
$stack = array(&$return);
foreach ( $items as $key => $item ) {
while ( $level >= $item['level'] ) {
unset($stack[$level--]);
}
$parent = &$stack[$level];
if ( !isset($parent['children']) ) {
$parent['children'] = array();
}
$parent['children'][] = &$items[$key];
if ( $item['level'] > $level ) {
$level = $item['level'];
$stack[$level] = &$items[$key];
}
}
return $return['children'];
}
带注释版本:
function stackItems( $items ){
$level = 0;
$return = array();
/// build our stack with our return array as first item
$stack = array(&$return);
/// step each item
foreach ( $items as $key => $item ) {
/// if our next item is lower down than current
/// level, drop the stack down.
while ( $level >= $item['level'] ) {
/// we may as well unset to clear memory
unset($stack[$level--]);
}
/// create a shortcut reference to our parent on the stack
$parent = &$stack[$level];
/// to avoid warnings create the children array first
if ( !isset($parent['children']) ) {
$parent['children'] = array();
}
/// now add ourselves to the current parent on the stack
$parent['children'][] = &$items[$key];
/// if our current item is higher level than current, add to
/// the stack ready to be treated as the next parent.
if ( $item['level'] > $level ) {
/// level ourselves to the current item
$level = $item['level'];
/// store in the stack a reference to item
$stack[$level] = &$items[$key];
}
}
/// return the first set of children as there seems
/// to be only one level 1 item.
return $return['children'];
}