In PHP arrays, how does one convert the same cid and add a new two-dimensional array field?

Source: Internet
Author: User
Array: {code...} How to convert the above array? {Code...} array:

Array ([0] => Array ([id] => 1 [menu_collapse] => Super menu [menu_items] => write an article [cid] => 1) [1] => Array ([id] => 2 [menu_collapse] => Maintenance Menu [menu_items] => Manage data [cid] => 2) [2] => Array ([id] => 3 [menu_collapse] => Maintenance Menu [menu_items] => Management user [cid] => 2 ))

How to convert the above array?

Array ([0] => Array ([id] => 1 [menu_collapse] => Super menu [menu_items] => write an article [cid] => 1) [1] => Array ([new] => Array ([id] => 2 [menu_collapse] => Maintenance Menu [menu_items] => Manage data [cid] => 2) ([id] => 2 [menu_collapse] => Maintenance Menu [menu_items] => Manage data [cid] => 2 ))

Reply content:

Array:

Array ([0] => Array ([id] => 1 [menu_collapse] => Super menu [menu_items] => write an article [cid] => 1) [1] => Array ([id] => 2 [menu_collapse] => Maintenance Menu [menu_items] => Manage data [cid] => 2) [2] => Array ([id] => 3 [menu_collapse] => Maintenance Menu [menu_items] => Management user [cid] => 2 ))

How to convert the above array?

Array ([0] => Array ([id] => 1 [menu_collapse] => Super menu [menu_items] => write an article [cid] => 1) [1] => Array ([new] => Array ([id] => 2 [menu_collapse] => Maintenance Menu [menu_items] => Manage data [cid] => 2) ([id] => 2 [menu_collapse] => Maintenance Menu [menu_items] => Manage data [cid] => 2 ))

$ Arr = [['id' => 1, 'menu _ collapse' => 'supermenu ', 'menu _ items' => 'write an write ', 'CID' => 1,], ['id' => 2, 'menu _ collapse' => 'maintenance menus ', 'menu _ items '=> 'Manage data', 'CID' => 2,], ['id' => 3, 'menu _ collapse' => 'maintenance menus ', 'menu _ items' => 'Manage users', 'CID' => 2,]; $ result = []; $ res = []; foreach ($ arr as $ k => $ v) {$ result [$ v ['CID'] [] = $ v ;} foreach ($ result as $ key => $ value) {if (count ($ value) = 1) {$ res [$ key] = $ value [0];} else {$ res [$ key] ['new'] = $ value ;}} echo'
'; Print_r (array_values ($ res )); /* Array ([0] => Array ([id] => 1 [menu_collapse] => Super menu [menu_items] => write an article [cid] => 1) [1] => Array ([new] => Array ([0] => Array ([id] => 2 [menu_collapse] => Maintenance Menu [menu_items] => Management data [cid] => 2) [1] => Array ([id] => 3 [menu_collapse] => Maintenance Menu [menu_items] => Management user [cid] => 2 ))))*/

You can only traverse the array for processing.

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.