code_junkie

PHP Arrays, appending depth of array item recursively to an array with the key of 'depth'

Nov 13th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. function array_set_depth($array, $depth = -1)
  2. {
  3. $subdepth = $depth + 1;
  4. if ($depth < 0) {
  5. foreach ($array as $key => $subarray) {
  6. $temp[$key] = array_set_depth(($subarray), $subdepth);
  7. }
  8. }
  9. if ($array['hasChildren'] && isset($array['children'])) {
  10. foreach ($array['children'] as $key => $subarray) {
  11. $temp[$key] = array_set_depth($subarray, $subdepth);
  12. }
  13. $array['children'] = $temp;
  14. }
  15. $array['depth'] = $depth;
  16. return $array;
  17. }
  18.  
  19. function array_set_depth($array, $depth = -1)
  20. {
  21. $subdepth = $depth + 1;
  22. if ($depth < 0) {
  23. foreach ($array as $key => $subarray) {
  24. $temp[$key] = array_set_depth(($subarray), $subdepth);
  25. }
  26. }
  27. if ($array['hasChildren'] && isset($array['children'])) {
  28. foreach ($array['children'] as $key => $subarray) {
  29. $temp[$key] = array_set_depth($subarray, $subdepth);
  30. }
  31. $array['children'] = $temp;
  32. }
  33. $array['depth'] = $depth;
  34. return $array;
  35. }
  36.  
  37. function array_set_depth($array, $depth = -1)
  38. {
  39. $subdepth = $depth + 1;
  40. if ($depth < 0) {
  41. foreach ($array as $key => $subarray) {
  42. $temp[$key] = array_set_depth(($subarray), $subdepth);
  43. }
  44. }
  45. if ($array['hasChildren'] && isset($array['children'])) {
  46. foreach ($array['children'] as $key => $subarray) {
  47. $temp[$key] = array_set_depth($subarray, $subdepth);
  48. }
  49. $array['children'] = $temp;
  50. }
  51. $array['depth'] = $depth;
  52. return $array;
  53. }
  54.  
  55. function array_set_depth($array, $depth = -1)
  56. {
  57. $subdepth = $depth + 1;
  58. if ($depth < 0) {
  59. foreach ($array as $key => $subarray) {
  60. $temp[$key] = array_set_depth(($subarray), $subdepth);
  61. }
  62. }
  63. if ($array['hasChildren'] && isset($array['children'])) {
  64. foreach ($array['children'] as $key => $subarray) {
  65. $temp[$key] = array_set_depth($subarray, $subdepth);
  66. }
  67. $array['children'] = $temp;
  68. }
  69. $array['depth'] = $depth;
  70. return $array;
  71. }
  72.  
  73. $b = array_set_depth($a);
  74. print_r($b);
  75.  
  76. function array_set_depth($array, $depth = -1)
  77. {
  78. $subdepth = $depth + 1;
  79. if ($depth < 0) {
  80. foreach ($array as $key => $subarray) {
  81. $temp[$key] = array_set_depth(($subarray), $subdepth);
  82. }
  83. return $temp;
  84. }
  85. $array['depth'] = $depth;
  86. if ($array['hasChildren'] && isset($array['children'])) {
  87. foreach ($array['children'] as $key => $subarray) {
  88. $temp[$key] = array_set_depth($subarray, $subdepth);
  89. }
  90. unset($array['children']);
  91. $array['children'] = $temp;
  92. }
  93. return $array;
  94. }
  95.  
  96. function setDepth(&$a, $depth)
  97. {
  98. $a['depth']=$depth;
  99. foreach($a as $key=>$value)
  100. {
  101. if (is_array($value))
  102. setDepth($a[$key], $depth+1);
  103. }
  104.  
  105. }
  106.  
  107. function setDepth(&$a, $depth = -1)
  108. {
  109. if (($depth > -1) && !($depth % 2))
  110. $a['depth']= $depth / 2;
  111. foreach($a as $key=>$value)
  112. {
  113. if (is_array($value))
  114. setDepth($a[$key], $depth+1);
  115. }
  116.  
  117. }
  118. setDepth($a);
  119. print_r($a);
  120.  
  121. function setdepth($arr, $depth = 0)
  122. {
  123. foreach ($arr as $key => $val)
  124. {
  125. $arr[$key]['depth'] = $depth;
  126. if ($arr[$key]['hasChildren'])
  127. {
  128. setdepth(&$arr[$key]['children'], $depth+1);
  129. }
  130. }
  131. }
  132.  
  133. $arr[0] = $website;
  134. setdepth(&$arr, 0);
Add Comment
Please, Sign In to add comment