【问题标题】:array_values recursive phparray_values 递归 php
【发布时间】:2012-08-10 05:39:26
【问题描述】:

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

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 的那个)

【问题讨论】:

  • 为什么人们坚持使用多层嵌套数组而不是使用 OOP?
  • 你给函数传入了什么,数组还是数组[children]?
  • 我将数组传递给函数

标签: php arrays recursion multidimensional-array


【解决方案1】:
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'
          )
);

【讨论】:

  • 你应该解释你的答案
【解决方案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;
}

【讨论】:

  • 递归有效,但我数组中的所有键都更改为 0、1 等...(甚至键 id 和名称)。我只希望数组中带有索引子项的数字键更改
  • 我现在只是把你的话翻译成PHP。
【解决方案3】:

应该这样做:

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

【讨论】:

    【解决方案4】:

    试试这个,

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

    【讨论】:

      【解决方案5】:

      你可以使用 php fnc walk_array_recursive Here

      【讨论】:

      • 请解释他们将如何使用它来解决他们的问题。
      • 我认为walk_array_recursive 不会越过树,而是会越过树叶。所以在这种情况下是不可用的。
      猜你喜欢
      • 2013-11-12
      • 2021-11-17
      • 2016-03-15
      • 2014-10-22
      • 2021-10-02
      • 1970-01-01
      • 2011-11-08
      • 2011-10-21
      相关资源
      最近更新 更多