Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

arrays - array_values recursive php

Let's say I have an array like this:

Array
(
    [id] => 45
    [name] => john
    [children] => Array
    (
        [45] => Array
            (
                [id] => 45
                [name] => steph
                [children] => Array
                    (
                        [56] => Array
                            (
                                [id] => 56
                                [name] => maria
                                [children] => Array
                                    (
                                        [60] => Array
                                            (
                                                [id] => 60
                                                [name] => thomas
                                            )

                                        [61] => Array
                                            (
                                                [id] => 61
                                                [name] => michelle
                                            )

                                    )
                            )

                        [57] => Array
                            (
                                [id] => 57
                                [name] => luis
                            )

                     )

            )

    )

)

What I'm trying to do is to reset the keys of the array with keys children to 0, 1, 2, 3, and so on, instead of 45, 56, or 57.

I tried something like:

function array_values_recursive($arr)
{
    foreach ($arr as $key => $value)
    {
        if(is_array($value))
        {
            $arr[$key] = array_values($value);
            $this->array_values_recursive($value);
        }
    }

    return $arr;
}

But that reset only the key of the first children array (the one with key 45)

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
function array_values_recursive($arr)
{
    $arr2=[];
    foreach ($arr as $key => $value)
    {
        if(is_array($value))
        {            
            $arr2[] = array_values_recursive($value);
        }else{
            $arr2[] =  $value;
        }
    }

    return $arr2;
}

this function that implement array_values_recursive from array like:

array(   
   'key1'=> 'value1',   
   'key2'=> array (              
         'key2-1'=>'value-2-1',
         'key2-2'=>'value-2-2'
          )
);

to array like:

array(
    0 => 'value1',
    1 => array (
           0 =>'value-2-1',
           1 =>'value-2-2'
          )
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...