How does the PHP array have the same CID transform and add new fields to the two-dimensional array?

Source: Internet
Author: User
Array:

Array(    [0] => Array        (            [id] => 1            [menu_collapse] => 超级菜单            [menu_items] => 写文章            [cid] => 1                 )    [1] => Array       (            [id] => 2            [menu_collapse] => 维护菜单            [menu_items] => 管理数据            [cid] => 2                 )    [2] => Array       (            [id] => 3            [menu_collapse] => 维护菜单            [menu_items] => 管理用户            [cid] => 2                 ))

How do I convert the above array to this?

Array(    [0] => Array        (            [id] => 1            [menu_collapse] => 超级菜单            [menu_items] => 写文章            [cid] => 1                 )    [1] => Array       (        [new] => Array                   (                        [id] => 2                        [menu_collapse] => 维护菜单                        [menu_items] => 管理数据                        [cid] => 2                                         )                 (                        [id] => 2                        [menu_collapse] => 维护菜单                        [menu_items] => 管理数据                        [cid] => 2                                        ))

Reply content:

Array:

Array(    [0] => Array        (            [id] => 1            [menu_collapse] => 超级菜单            [menu_items] => 写文章            [cid] => 1                 )    [1] => Array       (            [id] => 2            [menu_collapse] => 维护菜单            [menu_items] => 管理数据            [cid] => 2                 )    [2] => Array       (            [id] => 3            [menu_collapse] => 维护菜单            [menu_items] => 管理用户            [cid] => 2                 ))

How do I convert the above array to this?

Array(    [0] => Array        (            [id] => 1            [menu_collapse] => 超级菜单            [menu_items] => 写文章            [cid] => 1                 )    [1] => Array       (        [new] => Array                   (                        [id] => 2                        [menu_collapse] => 维护菜单                        [menu_items] => 管理数据                        [cid] => 2                                         )                 (                        [id] => 2                        [menu_collapse] => 维护菜单                        [menu_items] => 管理数据                        [cid] => 2                                        ))

 $arr = [[' id ' = + 1, ' menu_collapse ' = ' + ' super menu ', ' menu_items ' = ' = ' Write the article ', ' CID ' = 1,], [' id ' = + 2, ' menu_collapse ' = ' maintenance menu ', ' Menu_item S ' + ' management data ', ' cid ' = 2,], [' id ' = + 3, ' menu_collapse ' = ' + ' maintenance menu ', ' Menu_items ' + ' management user ', ' 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 ' 
 ';p Rint_r (array_values ($res));/*array ([0] = = Array (        [ID] + 1 [menu_collapse] + Super Menu [Menu_items] + write 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_co                        Llapse] = Maintenance Menu [Menu_items] + Admin user [CID] + 2 )                )        ))*/

You can only traverse the array to process it.

  • 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.