array_values递归php

问题描述 投票:5回答:5

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

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的键)

php arrays recursion multidimensional-array
5个回答
2
投票

您使用递归方法,但不指定函数调用$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;
}

3
投票
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'
          )
);

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

-1
投票

你可以使用php fnc walk_array_recursive Here

© www.soinside.com 2019 - 2024. All rights reserved.