降低子数组键并组合项目

我有一个数组


$arr = [

    'parent' => [

        'CHILD' => [

            5,6

        ],

        'child' => [

            1,2,3,4

        ],

        'Child' => [

            5,6,7,8

        ],

        ...

    ]

];

我想降低子键并组合每个具有相同不区分大小写键的子键


$arr = [

    'parent' => [

        'child' => [

            1,2,3,4,5,6,7,8

        ],

    ]

];

我试过array_change_key_case,它总是采用最后一个元素并忽略其他元素。


一个数组可能有多个具有相同键的孩子(大小写不同)


凤凰求蛊
浏览 68回答 1
1回答

慕村225694

试试下面的代码应该工作:<?php$arr = [&nbsp; &nbsp; 'parent' => [&nbsp; &nbsp; &nbsp; &nbsp; 'CHILD' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5,6&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; 'child' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1,2,3,4&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; ]];$arNew = [];foreach ($arr as $sParent => $ar) {&nbsp; &nbsp; foreach ($ar as $sChild => $ar1) {&nbsp; &nbsp; &nbsp; &nbsp; $sChild = strtolower($sChild);&nbsp; &nbsp; &nbsp; &nbsp; if (empty($arNew[$sParent][$sChild])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arNew[$sParent][$sChild] = $ar1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arNew[$sParent][$sChild] = array_merge($arNew[$sParent][$sChild], $ar1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}print_r($arNew);
打开App,查看更多内容
随时随地看视频慕课网APP