2017-03-19 3 views
0

Ich brauche Hilfe mit meinem Array. Für mehrere Stunden versuche ich eine Lösung zu finden, aber ich komme damit nicht zurecht. Ich habe versucht, es so zu machen, aber der Code erwies sich als völlig nutzlos.PHP Menü Generator mit multidimensionalen Array

private function findKey($array, $keySearch,$app,$klucz='') 
{ 
    foreach ($array as $key => $item) { 
     if ($key == $keySearch) { 
      $c2 = []; 
      $cache = [ 
       'nazwa'=>$app->nazwa, 
       'link'=>$app->link, 
       'child'=>[] 
      ]; 

      // echo $klucz; echo '<br />'; 

      if($klucz && $klucz != '/'){ 
       // echo $klucz; 
       // $k = explode('/',$klucz); 
       // $url = "/2/22/"; 
       // debug($klucz); 
       $parts = explode('/',$klucz); 
       debug($klucz); 
       debug($parts); 

       $x = 0; 
       $last = $parts[count($parts)-1]; 
       $arr = array(); 
       while ($bottom = array_pop($parts)) { 
        if($bottom != $last){ 

         $arr = array($bottom => $arr); 
         $x++; 
        }else{ 
         array_push($arr, $cache); 
        } 


       } 

       // $arr['child'] = $cache; 
       debug($arr); 
       // exit; 

       // print_r($output); 
       // exit; 

       // self::buildKey($k); 

       // print_r($c2); 

       echo "<br />"; 
      } 


     } 
     else { 
      $klucz = $klucz.'/'.$key; 
      if (is_array($item) && self::findKey($item, $keySearch,$app,$klucz)) { 
       // return true; 
      } 
     } 
    } 

    // return false; 
} 

Ich muss Array aus Schleife erstellen. Von mysql bekomme ich Daten als Objekt, und diese Datenansicht wie folgt:

| id | nazwa | link | eltern |

| 1 | abc | abcc | 0 |

| 2 | aaa | bbb | 1 |

| 3 | aas | bbc | 2 |

| 4 | asdasd | adsasd | 2 |

| 5 | asdasd | serae | 4 |

| 6 | rywer | twet | 0 |

Und jetzt brauche ich Array, deren Verwendung von Daten aus MySQL und zeigt es an Array wie folgt:

array(
    [1]=>array(
     id=>1, 
     nazwa=>abc 
     link=>abcc 
     child=>array(

      [2]=>array(
       id=>2, 
       nazwa=>aaa 
       link=>bbb 
       child=>array(

        [3]=>array(
         id=>3, 
         nazwa=>aas 
         link=>bbc 
         child=>array(

         ) 
        ), 

        [4]=>array(
         id=>4, 
         nazwa=>asdasd 
         link=>adsasd 
         child=>array(

          [5]=>array(
           id=>5, 
           nazwa=>asdasd 
           link=>serae 
           child=>array(

           ) 
          ), 
         ) 
        ), 
       ) 
      ) 
     ) 
    ), 

    [6]=>array(
     id=>6, 
     nazwa=>rywer 
     link=>twet 
     child=>array(

     ) 
    ), 
) 

Ich denke, nur eine einfache, gute Schleife oder eine Funktion, aber ich kann nicht mit ihm umgehen.

Antwort

1

Versuchen Sie meinen Code link to online demo:

<?php 
$data = array(
    array(
     'id' => 1, 
     'name' => 'abc', 
     'link' => 'abcc', 
     'parent' => 0 
    ), 
    array(
     'id' => 2, 
     'name' => 'aaa', 
     'link' => 'bbb', 
     'parent' => 1 
    ), 
    array(
     'id' => 3, 
     'name' => 'aas', 
     'link' => 'bbc', 
     'parent' => 2 
    ), 
    array(
     'id' => 4, 
     'name' => 'asdasd', 
     'link' => 'adsasd', 
     'parent' => 2 
    ), 
    array(
     'id' => 5, 
     'name' => 'asdasd', 
     'link' => 'serae', 
     'parent' => 4 
    ), 
    array(
     'id' => 6, 
     'name' => 'rywer', 
     'link' => 'twet', 
     'parent' => 0 
    ) 
); 

function buildMenu($data) { 
    usort($data, function($a, $b) { 
     if ($a['parent'] == $b['parent']) { 
      if ($a['id'] == $b['id']) { 
       return 0; 
      } 
      return ($a['id'] < $b['id']) ? -1 : 1; 
     } 
     return ($a['parent'] < $b['parent']) ? -1 : 1; 
    }); 

    $shortcuts = array(); 
    $menu = array(); 

    foreach($data as &$row) { 
     if ($row['parent'] <= 0) { 
      $menu[] = &$row; 
      $shortcuts[$row['id']] = &$row; 
      continue; 
     } else { 
      $parent = $row['parent']; 
      if (!isset($shortcuts[$parent])) { 
       throw new \Exception("Menu cannot be build"); 
      } 
      $parentItem = &$shortcuts[$parent]; 
     } 
     if (!isset($parentItem['child'])) { 
      $parentItem['child'] = array(); 
     } 
     $parentItem['child'][] = &$row; 
     $shortcuts[$row['id']] = &$row; 
    } 
    return $menu; 
} 

print_r(buildMenu($data)); 

Es Referenzen verwendet sie sauber zu halten. Zu Beginn der buildMenu-Funktion habe ich auch Ihr Sorce-Array so sortiert, dass Daten zunehmend nach Eltern-ID sortiert werden.

Bitte verwenden Sie auch englische Variablennamen.

Wenn Sie <ul> Menü aus diesem Array erzeugen möchten, verwenden Sie diesen Code:

$menu = '<ul>'; 
function buildUl($data) { 
    $menuHtml = ''; 
    foreach ($data as $menuItem) { 
     $menuHtml .= '<li>'; 
     $menuHtml .= '<a href="'.$menuItem['link'].'">'.$menuItem['name'].'</a>'; 
     if (!empty($menuItem['child'])) { 
      $menuHtml .= '<ul>'; 
      $menuHtml .= buildUl($menuItem['child']); 
      $menuHtml .= '</ul>'; 
     } 
     $menuHtml .= '</li>'; 
    } 

    return $menuHtml; 
} 
$menu .= buildUl(buildMenu($data)); 
$menu .= '</ul>'; 

echo $menu; 

Aktualisiert Beispiel: http://sandbox.onlinephpfunctions.com/code/27cfa95c066be9b1526b71566e2ec2f2093bdc34

+0

Danke, es funktioniert, aber wissen Sie, wie aus diesem Array generieren

    • ...
html code? :) – bradley546994

+0

Ja, ich habe meine Antwort mit einem Beispiel aktualisiert. Wie auch immer, Ihre Menüstruktur ist die hässlichste Art, geschachtelte Menüs zu erstellen. Bitte beachten Sie die Verwendung des Vororderbaums (allgemein bekannt als Links-Rechts-Baum). –

+0

danke! Du bist der beste :) – bradley546994

1

Die Dinge sind viel einfacher, wenn Sie Objekte verwenden:

$data = [ 
    ['id' => 1, 'nazwa' => 'abc', 'link' => 'abcc', 'parent' => 0], 
    ['id' => 2, 'nazwa' => 'aaa', 'link' => 'bbb', 'parent' => 1], 
    ['id' => 3, 'nazwa' => 'aas', 'link' => 'bbc', 'parent' => 2], 
    ['id' => 4, 'nazwa' => 'asdasd', 'link' => 'adsasd', 'parent' => 2], 
    ['id' => 5, 'nazwa' => 'asdasd', 'link' => 'serae', 'parent' => 4], 
    ['id' => 6, 'nazwa' => 'rywer', 'link' => 'twet', 'parent' => 0], 

]; 

$objectTree = []; 
$objects = []; 

foreach ($data as $row) { 
    $obj = (object)$row; 
    $obj->childs = []; 
    $objects[$obj->id] = $obj; 
} 

foreach ($objects as $obj) { 
    if ($obj->parent == 0) { 
     $objectTree[] = $obj; 
    } else { 
     $objects[$obj->parent]->childs[] = $obj; 
    } 
} 

var_export($objectTree); 

Demo: http://rextester.com/RZRQLX19028

Verwandte Themen