更改PHP中嵌套数组的第n级键索引格式

huangapple go评论66阅读模式
英文:

Change key indexing format for nth level of nested array in php

问题

[
    "NOCPL-All -working" => [
            "name" => "NOCPL-All -working",
            "item" => [
                        "0" => [ 
                            "name" => "apis for web",
                            "item" => [
                                    "0" => [
                                            "name" => "update user branch maps"
                                        ],
                                        "1" => [
                                            "name" => "add  user branch maps"
                                        ]
                            ]
                        ],
                        "1" => [ 
                            "name" => "update user web",
                            "item" => [
                                    "0" => [
                                            "name" => "update user"
                                        ]
                            ]
                        ]
                    ]
                ]
            ];
英文:

I have nth level of nested array with string naming key , I want to convert that in indexing key only for array of item key.
i tried it to convert but that conversation only possible for limited level of nested array rather than nth level .

Input array:

$input_array= [
    "NOCPL-All -working" => [
            "name" => "NOCPL-All -working",
            "item" => [
                        "apis for web" => [ 
                            "name" => "apis for web",
                            "item" => [
                                    "0" => [
                                            "name" => "update user branch maps"
                                        ]
                            ]
                        ],
                        "update user web" => [ 
                            "name" => "update user web",
                            "item" => [
                                    "0" => [
                                            "name" => "update user"
                                        ],
                                        "1" => [
                                            "name" => "add  user"
                                        ]
                            ]
                        ]
                    ]
                ]
            ];

I tried below code to convert indexing of 'item' nested array for limited level

function cleanArrayKeys($arr) {
        foreach($arr as $k=>$arr1) {
            if(isset($arr[$k]['item'])) {
                $arr[$k]['item'] = array_values($arr[$k]['item']);
                
                foreach($arr[$k]['item'] as $k1=>$arr2) {
                    if(isset($arr[$k]['item'][$k1]['item'])) {
                        $arr[$k]['item'][$k1]['item'] = array_values($arr[$k]['item'][$k1]['item']);
                        
                        foreach($arr[$k]['item'][$k1]['item'] as $k3=>$arr3) {
                            if(isset($arr[$k]['item'][$k1]['item'][$k3]['item'])) {
                                $arr[$k]['item'][$k1]['item'][$k3]['item'] = array_values($arr[$k]['item'][$k1]['item'][$k3]['item']);
                                
                                foreach($arr[$k]['item'][$k1]['item'][$k3]['item'] as $k4=>$arr4) {
                                    if(isset($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'])) {
                                        $arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'] = array_values($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item']);
                                        
                                        foreach($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'] as $k5=>$arr5) {
                                            if(isset($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'][$k5]['item'])) {
                                                $arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'][$k5]['item'] = array_values($arr[$k]['item'][$k1]['item'][$k3]['item'][$k4]['item'][$k5]['item']);
                                            }
                                        }
                                
                                    }
                                }
                            }
                        }
                
                    }
                }
            }
        }
        
        return $arr;
    }

print_r(cleanArrayKeys($input_array));
?>

Expected Output :

[
    "NOCPL-All -working" => [
            "name" => "NOCPL-All -working",
            "item" => [
                        "0" => [ 
                            "name" => "apis for web",
                            "item" => [
                                    "0" => [
                                            "name" => "update user branch maps"
                                        ],
                                        "1" => [
                                            "name" => "add  user branch maps"
                                        ]
                            ]
                        ],
                        "1" => [ 
                            "name" => "update user web",
                            "item" => [
                                    "0" => [
                                            "name" => "update user"
                                        ]
                            ]
                        ]
                    ]
                ]
            ];

答案1

得分: 1

尝试使用递归:

function aValues(&$arr) {
    if (array_key_exists('item', $arr)) {
        $arr['item'] = array_values($arr['item']);
    } 

    foreach ($arr['item'] as &$el) {
        if (array_key_exists('item', $el)) {
            aValues($el);
        }
    }
}

aValues($input_array);

print_r($input_array);
英文:

Try using a recursion:

function aValues(&$arr) {
    if (array_key_exists('item', $arr)) {
        $arr['item'] = array_values($arr['item']);
    } 

    foreach ($arr['item'] as &$el) {
        if (array_key_exists('item', $el)) {
            aValues($el);
        }
    }
}

aValues($input_array);

print_r($input_array);

huangapple
  • 本文由 发表于 2023年2月16日 13:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定