2009-01-12 132 views
4

根据最底层的示例数组,我希望能够追加数组内部的每个嵌入数组的深度。例如:PHP数组,使用'深度'键递归地添加数组元素的深度

 
    array (
     53 => 
     array (
     'title' => 'Home', 
     'path' => '', 
     'type' => '118', 
     'pid' => 52, 
     'hasChildren' => 0, 
    ), 

已按如下所以现在应该是这样的样品阵列一个深度:

 
    array (
     53 => 
     array (
     'title' => 'Home', 
     'path' => '', 
     'type' => '118', 
     'pid' => 52, 
     'hasChildren' => 0, 
     'depth' => 1, 
    ), 

等等...

所有的我所做的递归数组函数尝试都很尴尬。不过,我已经看过具有getDepth函数的RecursiveArrayIterator。我很困惑如何将它附加到当前数组...任何帮助非常感谢,谢谢。

 
    
    array (
    'title' => 'Website Navigation', 
    'path' => '', 
    'type' => '115', 
    'pid' => 0, 
    'hasChildren' => 1, 
    'children' => 
    array (
     53 => 
     array (
     'title' => 'Home', 
     'path' => '', 
     'type' => '118', 
     'pid' => 52, 
     'hasChildren' => 0, 
    ), 
     54 => 
     array (
     'title' => 'Features', 
     'path' => 'features', 
     'type' => '374', 
     'pid' => 52, 
     'hasChildren' => 1, 
     'children' => 
     array (
      59 => 
      array (
      'title' => 'artistic', 
      'path' => 'features/artistic', 
      'type' => '374', 
      'pid' => 54, 
      'hasChildren' => 1, 
      'children' => 
      array (
       63 => 
       array (
       'title' => 'galleries', 
       'path' => 'features/artistic/galleries', 
       'type' => '374', 
       'pid' => 59, 
       'hasChildren' => 1, 
       'children' => 
       array (
        65 => 
        array (
        'title' => 'graphics', 
        'path' => 'features/artistic/galleries/graphics', 
        'type' => '118', 
        'pid' => 63, 
        'hasChildren' => 0, 
       ), 
        67 => 
        array (
        'title' => 'mixed medium', 
        'path' => 'features/artistic/galleries/mixed-medium', 
        'type' => '118', 
        'pid' => 63, 
        'hasChildren' => 0, 
       ), 
        64 => 
        array (
        'title' => 'overview', 
        'path' => 'features/artistic/galleries', 
        'type' => '118', 
        'pid' => 63, 
        'hasChildren' => 0, 
       ), 
        68 => 
        array (
        'title' => 'photography', 
        'path' => 'features/artistic/galleries/photography', 
        'type' => '118', 
        'pid' => 63, 
        'hasChildren' => 0, 
       ), 
        66 => 
        array (
        'title' => 'traditional', 
        'path' => 'features/artistic/galleries/traditional', 
        'type' => '118', 
        'pid' => 63, 
        'hasChildren' => 0, 
       ), 
       ), 
      ), 
       62 => 
       array (
       'title' => 'overview', 
       'path' => 'features/artistic', 
       'type' => '118', 
       'pid' => 59, 
       'hasChildren' => 0, 
      ), 
       69 => 
       array (
       'title' => 'tutorials', 
       'path' => 'features/artistic/tutorials', 
       'type' => '374', 
       'pid' => 59, 
       'hasChildren' => 1, 
       'children' => 
       array (
        71 => 
        array (
        'title' => 'by category', 
        'path' => 'features/artistic/tutorials/by-category/', 
        'type' => '118', 
        'pid' => 69, 
        'hasChildren' => 0, 
       ), 
        72 => 
        array (
        'title' => 'by date', 
        'path' => 'features/artistic/tutorials/by-date/', 
        'type' => '118', 
        'pid' => 69, 
        'hasChildren' => 0, 
       ), 
        70 => 
        array (
        'title' => 'overview', 
        'path' => 'features/artistic/tutorials', 
        'type' => '118', 
        'pid' => 69, 
        'hasChildren' => 0, 
       ), 
       ), 
      ), 
      ), 
     ), 
      58 => 
      array (
      'title' => 'overview', 
      'path' => 'features', 
      'type' => '118', 
      'pid' => 54, 
      'hasChildren' => 0, 
     ), 
      61 => 
      array (
      'title' => 'projects/labs', 
      'path' => 'features/projects-labs/', 
      'type' => '374', 
      'pid' => 54, 
      'hasChildren' => 0, 
     ), 
      60 => 
      array (
      'title' => 'web development', 
      'path' => 'features/web-development', 
      'type' => '374', 
      'pid' => 54, 
      'hasChildren' => 1, 
      'children' => 
      array (
       74 => 
       array (
       'title' => 'articles', 
       'path' => 'features/web-development/articles/', 
       'type' => '374', 
       'pid' => 60, 
       'hasChildren' => 0, 
      ), 
       73 => 
       array (
       'title' => 'overview', 
       'path' => 'features/web-development', 
       'type' => '118', 
       'pid' => 60, 
       'hasChildren' => 0, 
      ), 
       75 => 
       array (
       'title' => 'tutorials', 
       'path' => 'features/web-development/tutorials', 
       'type' => '374', 
       'pid' => 60, 
       'hasChildren' => 0, 
      ), 
      ), 
     ), 
     ), 
    ), 
     55 => 
     array (
     'title' => 'Activity', 
     'path' => 'activity', 
     'type' => '374', 
     'pid' => 52, 
     'hasChildren' => 0, 
    ), 
     56 => 
     array (
     'title' => 'Blog', 
     'path' => 'blog', 
     'type' => '374', 
     'pid' => 52, 
     'hasChildren' => 0, 
    ), 
     57 => 
     array (
     'title' => 'About', 
     'path' => 'about', 
     'type' => '374', 
     'pid' => 52, 
     'hasChildren' => 1, 
     'children' => 
     array (
      76 => 
      array (
      'title' => 'the author', 
      'path' => 'about/the-author', 
      'type' => '118', 
      'pid' => 57, 
      'hasChildren' => 0, 
     ), 
      77 => 
      array (
      'title' => 'the website', 
      'path' => 'about/the-website', 
      'type' => '118', 
      'pid' => 57, 
      'hasChildren' => 0, 
     ), 
     ), 
    ), 
    ), 
), 
); 

print_r($example); 

?> 

回答

8

我假设有另一个数组(顶部不包括在您的示例代码中) 是这样的吗?

function array_set_depth($array, $depth = -1) 
{ 
    $subdepth = $depth + 1; 
    if ($depth < 0) { 
    foreach ($array as $key => $subarray) { 
     $temp[$key] = array_set_depth(($subarray), $subdepth); 
    } 
    } 
    if ($array['hasChildren'] && isset($array['children'])) { 
    foreach ($array['children'] as $key => $subarray) { 
     $temp[$key] = array_set_depth($subarray, $subdepth); 
    } 
    $array['children'] = $temp; 
    } 
    $array['depth'] = $depth; 
    return $array; 
} 

用法示例,我设置你的阵列值$一个:

$b = array_set_depth($a); 
print_r($b); 

编辑:

为了孩子的美好印刷可以做到这一点之前设置深度:

function array_set_depth($array, $depth = -1) 
{ 
    $subdepth = $depth + 1; 
    if ($depth < 0) { 
    foreach ($array as $key => $subarray) { 
     $temp[$key] = array_set_depth(($subarray), $subdepth); 
    } 
    return $temp; 
    } 
    $array['depth'] = $depth; 
    if ($array['hasChildren'] && isset($array['children'])) { 
    foreach ($array['children'] as $key => $subarray) { 
     $temp[$key] = array_set_depth($subarray, $subdepth); 
    } 
    unset($array['children']); 
    $array['children'] = $temp; 
    } 
    return $array; 
} 
5

像这样的递归函数应该这样做吗?

function setDepth(&$a, $depth) 
{ 
    $a['depth']=$depth; 
    foreach($a as $key=>$value) 
    { 
     if (is_array($value)) 
      setDepth($a[$key], $depth+1); 
    } 

} 

需要注意的是该数组通过引用传递,以便我们可以对其进行修改。请注意,我们也在setDepth的递归调用中使用此引用。尽管为了方便我使用了foreach,但$ value变量是一个副本,并将其传递给setDepth只会在foreach循环的范围内进行短暂的更改。

+0

这是一个数组的通用递归函数。这个例子需要一些工作。 – OIS 2009-01-12 13:12:38

+0

我修改了你的代码,就像它应该那样工作。不知道如何发布。 – OIS 2009-01-12 13:19:00

0

某事像这样应该做的伎俩:

function setdepth($arr, $depth = 0) 
{ 
    foreach ($arr as $key => $val) 
    { 
     $arr[$key]['depth'] = $depth; 
     if ($arr[$key]['hasChildren']) 
     { 
      setdepth(&$arr[$key]['children'], $depth+1); 
     } 
    } 
} 

我会更容易些,如果你的阵列开始与指数不值,因此例如使用可以是这样的:

$arr[0] = $website; 
setdepth(&$arr, 0); 

即网站是从你的例子

4

修改过的Pauls代码来处理这个例子。

function setDepth(&$a, $depth = -1) 
{ 
    if (($depth > -1) && !($depth % 2)) 
     $a['depth']= $depth/2; 
    foreach($a as $key=>$value) 
    { 
     if (is_array($value)) 
      setDepth($a[$key], $depth+1); 
    } 

} 
setDepth($a); 
print_r($a); 
0

这可能会有所帮助:

function extend($arr, $myArr=array()) { 

    foreach($arr as $key => $value) { 
     if(is_array($key)) { 
      extend($arr[ $key ]); 
     } else { 
      $myArr[ $key ] = $arr[ $key ]; 
     } 
    } 

    return $myArr; 
} 

功能称为“扩展”,因为它不仅是复制数组新的,它也可以扩展现有阵列。

要扩展一个数组,你应该把它作为第二个参数,否则放一个空数组。 该函数通过数组属性检查,并检查它是否是一个数组,如果它是函数再次调用,否则它将值复制到另一个数组并返回它。