2016-10-05 42 views
0

私は以下のPHPコードで示された状況を持っており、check_recursive()という再帰関数を作りたいと思っています。PHPの配列から再帰関数を作成する

以下のcheck_recursive()関数を作成しましたが、可能ならば再帰関数が必要です。

ありがとうございました!

$menu = '[{"id":"3|case_studies","children":[{"id":"2|case_studies","children":[{"id":"1|custom_links","children":[{"id":"2|faqe"}]}]}]},{"id":"11|klientet","children":[{"id":"8|klientet","children":[{"id":"7|klientet"}]}]},{"id":"9|klientet","children":[{"id":"10|klientet"}]},{"id":"4|klientet"}]'; 

    $old_menu = json_decode($menu, true); 

    $new_menu = $this->check_recursive($old_menu); 

    function check_recursive($old_menu) 
    { 
     $i = 0; 
     $new_menu = []; 
     foreach ($old_menu as $menu_item) 
     { 
      if($name = $this->check_menu($menu_item['id'])) 
      { 
       $new_menu[$i]['id'] = $menu_item['id'] . '|' . $name; 
       if(isset($menu_item['children'])) 
       { 
        $e = 0; 
        foreach ($menu_item['children'] as $menu_item) 
        { 
         if($name = $this->check_menu($menu_item['id'])) 
         { 
          $new_menu[$i]['children'][$e]['id'] = $menu_item['id'] . '|' . $name; 
          if(isset($menu_item['children'])) 
          { 
           $y = 0; 
           foreach ($menu_item['children'] as $menu_item) 
           { 
            if($name = $this->check_menu($menu_item['id'])) 
            { 
             $new_menu[$i]['children'][$e]['children'][$y]['id'] = $menu_item['id'] . '|' . $name; 
             if(isset($menu_item['children'])) 
             { 
              $a = 0; 
              foreach ($menu_item['children'] as $menu_item) 
              { 
               if($name = $this->check_menu($menu_item['id'])) 
               { 
                $new_menu[$i]['children'][$e]['children'][$y]['children'][$a]['id'] = $menu_item['id'] . '|' . $name; 
               } 
               $a++; 
              } 
             } 
            } 
            $y++; 
           } 
          } 
         } 
         $e++; 
        } 
       } 
      } 
      $i++; 
     } 
     return $new_menu; 
    } 

    function check_menu($string){ 
     //Check if string exists in database 
     if($string){ 
      return 'String exists'; 
     } 
     return false; 
    } 

答えて

0

私はこの思い付いた:

$menu = '[{"id":"3|case_studies","children":[{"id":"2|case_studies","children":[{"id":"1|custom_links","children":[{"id":"2|faqe"}]}]}]},{"id":"11|klientet","children":[{"id":"8|klientet","children":[{"id":"7|klientet"}]}]},{"id":"9|klientet","children":[{"id":"10|klientet"}]},{"id":"4|klientet"}]'; 

$old_menu = json_decode($menu, true); 

$new_menu = check_recursive($old_menu); 

function check_recursive($old_menu) 
{ 
    $new_menu = array(); 

    foreach ($old_menu as $item) { 
     $name = check_menu($item['id']); 
     if($name){ 
      $new_item = array(
       'id' => $item['id'] . '|' . $name, 
      ); 

      if(isset($item['children'])){ 
       $new_item['children'] = check_recursive($item['children']); 
      } 

      $new_menu[] = $new_item; 
     } 

    } 

    return $new_menu; 
} 

function check_menu($string) 
{ 
    //Check if string exists in database 
    if ($string) { 
     return 'String exists'; 
    } 
    return false; 
} 

は、それはあなたのニーズに合った場合、私に教えてください。

+0

ありがとう@vhoen素晴らしいです! –

関連する問題