Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

php - Reset array keys in multidimensional array

I've been looking around for a solution to this with no real success. I have a multidimensional array of parents and children with no limits on depth. This is generated from a database but the issue is that the item ID becomes the key using my way of arranging a flat array into a multidimensional array like so:

Array(

[28] => Array
        (
            [id] => 28
            [color] => #ff24e5
            [name] => Personal
            [parent_id] => 
            [children] => Array
                (
                    [23] => Array
                        (
                            [id] => 23
                            [color] => #41c3a3
                            [name] => Shopping
                            [parent_id] => 28
                            [children] => Array
                                (
                                    [22] => Array
                                        (
                                            [id] => 22
                                            [color] => #8be32b
                                            [name] => Deals
                                            [parent_id] => 23
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )

                    [150] => Array
                        (
                            [id] => 150
                            [color] => #e9a3f0
                            [name] => Orders
                            [parent_id] => 28
                            [children] => Array
                                (
                                )
                        )
                )
        )
)

What I would like, is a function that does the following:

Array (
[0] => Array
        (
            [id] => 28
            [color] => #ff24e5
            [name] => Personal
            [parent_id] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 23
                            [color] => #41c3a3
                            [name] => Shopping
                            [parent_id] => 28
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 22
                                            [color] => #8be32b
                                            [name] => Deals
                                            [user_id] => 1
                                            [selected] => 0
                                            [parent_id] => 23
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )

                    [1] => Array
                        (
                            [id] => 150
                            [color] => #e9a3f0
                            [name] => Orders
                            [parent_id] => 28
                            [children] => Array
                                (
                                )
                        )
                )
        )
)

Essentially reassign keys starting from 0. I've tried numerous methods, but I'm assuming that I need to find a recursive solution and when I tried that, it destroyed my array. I was reading up on the array_walk_recursive() function, but I don't quite know what to do beyond that. Essentially, is there a way to reset numeric keys in a multidimensional array?

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You really need to add the is_numeric condition to stop text keys getting mixed up...

function fix_keys($array) {

    foreach ($array as $k => $val) {

        if (is_array($val)) 
            $array[$k] = $fix_keys($val); //recurse
    }

    if( is_numeric($k) )
        return array_values($array);

    return $array;
}

I did this instead:

function fix_keys($array) {
    $numberCheck = false;
    foreach ($array as $k => $val) {
        if (is_array($val)) $array[$k] = fix_keys($val); //recurse
        if (is_numeric($k)) $numberCheck = true;
    }
    if ($numberCheck === true) {
        return array_values($array);
    } else {
        return $array;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...