PHP中array_values递归的含义是什么?

8
假设我有一个像这样的数组:

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
                            )

                     )

            )

    )

)

我将要做的是将数组中键名为 children 的键重置为0、1、2、3等顺序排列,而不是45、56或57等。我尝试过以下方法:
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;
}

但这只重置了第一个子数组的键(键为45的那个)


2
为什么人们坚持使用多层嵌套数组而不是使用面向对象编程呢? - Nir Alfasi
你传递给函数的是数组还是array[children]? - gcochard
我将数组传递给了函数。 - user765368
5个回答

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

这个函数实现了 array_values_recursive,适用于类似以下结构的数组:
array(   
   'key1'=> 'value1',   
   'key2'=> array (              
         'key2-1'=>'value-2-1',
         'key2-2'=>'value-2-2'
          )
);

转换为类似数组的形式:

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

你应该解释你的答案 - zoran404

3
您使用了递归方法,但是没有将函数调用 $this->array_values_recursive($value); 的返回值赋给任何变量。第一层可以正常工作,因为您在循环中修改了 $arr。但由于上述原因,进一步的递归层级将无法正常工作。
如果您想保留该函数,请按以下方式更改(未经测试):
function array_values_recursive($arr)
{
  foreach ($arr as $key => $value)
  {
    if (is_array($value))
    {
      $arr[$key] = $this->array_values_recursive($value);
    }
  }

  if (isset($arr['children']))
  {
    $arr['children'] = array_values($arr['children']);
  }

  return $arr;
}

递归函数可以正常工作,但是我的数组中所有的键都被更改为0、1等(甚至包括id和name键)。我只想要索引为children的数字键被更改。 - user765368
我现在只是把你的话翻译成了 PHP。 - Shi

0

应该可以了:

function array_values_recursive($arr, $key)
{
    $arr2 = ($key == 'children') ? array_values($arr) : $arr;
    foreach ($arr2 as $key => &$value)
    {
        if(is_array($value))
        {
            $value = array_values_recursive($value, $key);
        }
    }
    return $arr2;
}

0

试试这个,

    function updateData($updateAry,$result = array()){
     foreach($updateAry as $key => $values){
      if(is_array($values) && count($values) > 0){
        $result[$key] = $this->_updateData($values,$values);
      }else{
        $result[$key] = 'here you can update values';
      }
     }
     return $result;
   }

-2
你可以使用 PHP 函数 walk_array_recursive。 这里

请解释一下他们如何使用它来解决他们的问题。 - Chai T. Rex
我认为 walk_array_recursive 不会遍历整个树,而是只会遍历叶子节点。因此在这种情况下无法使用。 - UselesssCat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接