Home > Backend Development > PHP Tutorial > How to convert a PHP array with the same cid and add a new field to a new two-dimensional array?

How to convert a PHP array with the same cid and add a new field to a new two-dimensional array?

WBOY
Release: 2023-03-01 22:38:01
Original
992 people have browsed it

Array:

<code>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
         
        )

)</code>
Copy after login
Copy after login

How to convert the above array into this?

<code>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
                    
                    )

)</code>
Copy after login
Copy after login

Reply content:

Array:

<code>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
         
        )

)</code>
Copy after login
Copy after login

How to convert the above array into this?

<code>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
                    
                    )

)</code>
Copy after login
Copy after login

<code class="php">
$arr = [
['id' => 1,
            'menu_collapse' => '超级菜单',
            'menu_items' => '写文章',
            'cid' => 1,
         
        ],

   [ 'id' => 2,
            'menu_collapse' => '维护菜单',
            'menu_items' => '管理数据',
            'cid' => 2,
         
        ],

    [  'id' => 3,
            'menu_collapse' => '维护菜单',
            'menu_items' => '管理用户',
            '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 '<pre class="brush:php;toolbar:false">';print_r(array_values($res));
/*
Array
(
    [0] => Array
        (
            [id] => 1
            [menu_collapse] => 超级菜单
            [menu_items] => 写文章
            [cid] => 1
        )

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

                    [1] => Array
                        (
                            [id] => 3
                            [menu_collapse] => 维护菜单
                            [menu_items] => 管理用户
                            [cid] => 2
                        )

                )

        )

)
*/</code>
Copy after login

You can only process it by traversing the array.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template