PHP 배열을 추가하여 깊이의 배열에 항목을 재귀적으로 배의 키와'깊이'

StackOverflow https://stackoverflow.com/questions/435180

문제

당 예제는 배열 맨 아래에,내가 할 수 있 추가의 깊이 포함된 각 배열 내의 배열입니다.예를 들어:

    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,
      ),

그래서...

의 모든 재귀적 배열 기능도 내가 만들은 매우 당황합니다.그러나 나는 RecursiveArrayIterator 는 getDepth 기능입니다.내가 혼동하는 방법에 대한 추가 그것을 현재 array...어떤 도움을 매우 많이 감사,감사합니다.

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

?>
도움이 되었습니까?

해결책

나는 가정이 또 다른 배열(상단에 포함되지 않습니다 예제 코드입니다.무언가가 이것을 좋아하는가?

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

다른 팁

재귀 같은 기능을 이해야 할까?

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 다.

수정된 폴스 코드를 작동으로 이것은 예입니다.

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

sth 다음과 같이 할 수 있습니다.:

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

웹사이트 배열에서 당신의 예

이 도움이 될 수 있다:

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

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

    return $myArr;
}

기능"이라고 확장하기 때문에"그것뿐만 아니라 복사열로 새로운 하나,그것은 또한 것이다:기존 배열입니다.

을 확장하 배열해야 합 넣어 두 번째 매개 변수로,그렇지 않으면 빈 배열입니다.기능 lopps 배열을 통해와 속성을 확인 그것은 배열하고 있는지는 기능 envoked 다시 그렇지 않으면 그것은 사본으로 값을 다른 배열을 반환합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top