重置多维数组中的数组键

时间:2021-01-12 10:45:52

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:

我一直在寻找解决方案,但没有取得真正的成功。我有一个多维的父母和孩子阵列,对深度没有限制。这是从数据库生成的,但问题是项目ID成为关键,使用我的方式将平面数组排列成多维数组,如下所示:

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?

基本上从0开始重新分配密钥。我尝试了很多方法,但我假设我需要找到一个递归解决方案,当我尝试它时,它会破坏我的数组。我正在阅读array_walk_recursive()函数,但我不知道该怎么做。基本上,有没有办法重置多维数组中的数字键?

Thanks for the help!

谢谢您的帮助!

4 个解决方案

#1


12  

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

#2


11  

I was trying to fix the same problem, here is the code

我试图解决同样的问题,这是代码

$array = array_values($array);

#3


10  

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

你真的需要添加is_numeric条件来阻止文本键混淆...

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;
    }
}

#4


0  

the correct answer reset all keys and dont ignore the not numeric keys, "Lobos" answer is a step in the right direction, but reset too the not numeric keys by level 2 and lower. for me this done the job perfect

正确答案重置所有键并且不要忽略非数字键,“Lobos”答案是向正确方向迈出的一步,但是按2级和更低级别重置非数字键。对我来说,这完成了工作

function array_values_recursive($array) {
$temp = array();
foreach ($array as $key => $value) {
    if (is_numeric($key)) {
        $temp[] = is_array($value) ? array_values_recursive($value) : $value;
    } else {
        $temp[$key] = is_array($value) ? array_values_recursive($value) : $value;
    }
}
return $temp;

}

}

#1


12  

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

#2


11  

I was trying to fix the same problem, here is the code

我试图解决同样的问题,这是代码

$array = array_values($array);

#3


10  

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

你真的需要添加is_numeric条件来阻止文本键混淆...

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;
    }
}

#4


0  

the correct answer reset all keys and dont ignore the not numeric keys, "Lobos" answer is a step in the right direction, but reset too the not numeric keys by level 2 and lower. for me this done the job perfect

正确答案重置所有键并且不要忽略非数字键,“Lobos”答案是向正确方向迈出的一步,但是按2级和更低级别重置非数字键。对我来说,这完成了工作

function array_values_recursive($array) {
$temp = array();
foreach ($array as $key => $value) {
    if (is_numeric($key)) {
        $temp[] = is_array($value) ? array_values_recursive($value) : $value;
    } else {
        $temp[$key] = is_array($value) ? array_values_recursive($value) : $value;
    }
}
return $temp;

}

}