PHP数组,使用 'depth'的键将数组项的深度递归地添加到数组中

标签 php arrays recursion associative-array

根据最底部的示例数组,我希望能够在数组内部附加每个嵌入式数组的深度。例如:

大批 (
53 =>
大批 (
'title'=>'Home',
'路径'=>'',
'类型'=>'118',
'pid'=> 52,
'hasChildren'=> 0,
),

根据下面显示的示例数组,其深度为1,因此它现在应如下所示:

大批 (
53 =>
大批 (
'title'=>'Home',
'路径'=>'',
'类型'=>'118',
'pid'=> 52,
'hasChildren'=> 0,
'深度'=> 1,
),

等等...

我进行的所有递归数组函数尝试都非常尴尬。但是我看过具有getDepth函数的RecursiveArrayIterator。我对如何将其附加到当前数组感到困惑...非常感谢您的任何帮助。


大批 (
'title'=>'网站导航',
'路径'=>'',
'类型'=>'115',
'pid'=> 0,
'hasChildren'=> 1,
' child '=>
大批 (
53 =>
大批 (
'title'=>'Home',
'路径'=>'',
'类型'=>'118',
'pid'=> 52,
'hasChildren'=> 0,
),
54 =>
大批 (
'标题'=>'功能',
'路径'=>'功能',
'类型'=>'374',
'pid'=> 52,
'hasChildren'=> 1,
' child '=>
大批 (
59 =>
大批 (
'title'=>'artistic',
'路径'=>'功能/艺术性',
'类型'=>'374',
'pid'=> 54
'hasChildren'=> 1,
' child '=>
大批 (
63 =>
大批 (
'title'=>'galleries',
'路径'=>'功能/艺术/画廊',
'类型'=>'374',
'pid'=> 59,
'hasChildren'=> 1,
' child '=>
大批 (
65 =>
大批 (
'标题'=>'图形',
'路径'=>'功能/艺术/图库/图形',
'类型'=>'118',
'pid'=> 63,
'hasChildren'=> 0,
),
67 =>
大批 (
'标题'=>'混合媒介',
'路径'=>'功能/艺术/画廊/混合媒介',
'类型'=>'118',
'pid'=> 63,
'hasChildren'=> 0,
),
64 =>
大批 (
'标题'=>'概述',
'路径'=>'功能/艺术/画廊',
'类型'=>'118',
'pid'=> 63,
'hasChildren'=> 0,
),
68 =>
大批 (
'title'=>'photography',
'路径'=>'功能/艺术/画廊/摄影',
'类型'=>'118',
'pid'=> 63,
'hasChildren'=> 0,
),
66 =>
大批 (
'title'=>'传统',
'路径'=>'功能/艺术/画廊/传统',
'类型'=>'118',
'pid'=> 63,
'hasChildren'=> 0,
),
),
),
62 =>
大批 (
'标题'=>'概述',
'路径'=>'功能/艺术性',
'类型'=>'118',
'pid'=> 59,
'hasChildren'=> 0,
),
69 =>
大批 (
'title'=>'教程',
'路径'=>'功能/艺术/教程',
'类型'=>'374',
'pid'=> 59,
'hasChildren'=> 1,
' child '=>
大批 (
71 =>
大批 (
'标题'=>'按类别',
'路径'=>'功能/艺术/教程/按类别/',
'类型'=>'118',
'pid'=> 69,
'hasChildren'=> 0,
),
72 =>
大批 (
'title'=>'按日期',
'路径'=>'功能/艺术/教程/按日期/',
'类型'=>'118',
'pid'=> 69,
'hasChildren'=> 0,
),
70 =>
大批 (
'标题'=>'概述',
'路径'=>'功能/艺术/教程',
'类型'=>'118',
'pid'=> 69,
'hasChildren'=> 0,
),
),
),
),
),
58 =>
大批 (
'标题'=>'概述',
'路径'=>'功能',
'类型'=>'118',
'pid'=> 54
'hasChildren'=> 0,
),
61 =>
大批 (
'title'=>'项目/实验室',
'路径'=>'功能/项目实验室/',
'类型'=>'374',
'pid'=> 54
'hasChildren'=> 0,
),
60 =>
大批 (
'title'=>'网络开发',
'路径'=>'功能/网络开发',
'类型'=>'374',
'pid'=> 54
'hasChildren'=> 1,
' child '=>
大批 (
74 =>
大批 (
'title'=>'articles',
'路径'=>'功能/网络开发/文章/',
'类型'=>'374',
'pid'=> 60,
'hasChildren'=> 0,
),
73 =>
大批 (
'标题'=>'概述',
'路径'=>'功能/网络开发',
'类型'=>'118',
'pid'=> 60,
'hasChildren'=> 0,
),
75 =>
大批 (
'title'=>'教程',
'路径'=>'功能/网络开发/教程',
'类型'=>'374',
'pid'=> 60,
'hasChildren'=> 0,
),
),
),
),
),
55 =>
大批 (
'标题'=>'事件',
'路径'=>'事件',
'类型'=>'374',
'pid'=> 52,
'hasChildren'=> 0,
),
56 =>
大批 (
'标题'=>'博客',
'路径'=>'博客',
'类型'=>'374',
'pid'=> 52,
'hasChildren'=> 0,
),
57 =>
大批 (
'标题'=>'关于',
'路径'=>'关于',
'类型'=>'374',
'pid'=> 52,
'hasChildren'=> 1,
' child '=>
大批 (
76 =>
大批 (
'title'=>'作者',
'路径'=>'关于/作者',
'类型'=>'118',
'pid'=> 57,
'hasChildren'=> 0,
),
77 =>
大批 (
'标题'=>'网站',
'路径'=>'关于/网站',
'类型'=>'118',
'pid'=> 57,
'hasChildren'=> 0,
),
),
),
),
),
);

print_r($ example);

?>

最佳答案

我假设还有另一个数组(在您的示例代码中没有包括在顶部。
像这样吗?

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

用法示例,我将您的数组设置为值$ a:
$b = array_set_depth($a);
print_r($b);

编辑:

要在 children 进行 pretty-print 之前设置深度,您可以执行以下操作:
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;
}

关于PHP数组,使用 'depth'的键将数组项的深度递归地添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/435180/

相关文章:

javascript - 在 Javascript 中按自定义顺序放置数组元素的最佳方法

c - 修改指向整数数组的双指针

c - C中的二项式系数递归解

php - 在 PDO 中获取 SUM

php - PHP从视频中提取音频

javascript - 返回数组中的第一个和最后一个值

javascript - 不创建新节点的递归后序树遍历

php - 使用 PHP 将 mySQL 转换为 XLS?

php - 在 Linux 中运行 Php 代码

python - 通过排列过滤