Menu aus array erstellt - ein Fehler - wo ist der?

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Menu aus array erstellt - ein Fehler - wo ist der?

    Dieser code erzeugt ein Menü mit Unterpunkte. Was ich nicht verstehe ist, warum der Link zu "B 1" als einziger als Array und nicht als Wert angezeigt wird.

    PHP-Code:
    <?php

    $PHP_SELF 
    $_SERVER['PHP_SELF'];
    $pfad="/get/menu/";
    $menu=array(
    "Home"=>array("root"=>$pfad."index.php"),
    "A"=>array("root"=>$pfad."a.php",
        
    "A 1"=>$pfad."a1.php",
        
    "A 2"=>$pfad."a2.php"),
    "B"=>array("root"=>$pfad."b.php",
                        
    "B 1"=>array("root"=>"b1.php""B 2"=>$pfad."b2.php")),
    "C"=>array("root"=>$pfad."c.php")
    );


      foreach(
    $menu as $key=>$value){
           echo 
    "<a href=".$value['root'].">$key</a><br>";

                foreach(
    $value as $key2=>$value2){
                    if(
    $key2 != "root")
                        echo 
    " - <a href=".$value2.">$key2</a><br>\n";
                    }

                        if(
    is_array($value2)){

                            foreach(
    $value2 as $key3=>$value3){
                                if(
    $key3 != "root"  ){
                                    echo 
    " - - <a href=".$value3.">$key3</a><br>\n";
                                }

                            }

                  }


        }

    ?>
    Sieht jemand den Fehler?

  • #2
    Weil das Element B 1 ein Array ist! Vergleiche mal wie du es bei A gemacht hast, dann sollte dir der Unterschied auffallen.

    Gruss

    tobi
    Gutes Tutorial | PHP Manual | MySql Manual | PHP FAQ | Apache | Suchfunktion für eigene Seiten

    [color=red]"An error does not become truth by reason of multiplied propagation, nor does truth become error because nobody sees it."[/color]
    Mohandas Karamchand Gandhi (Mahatma Gandhi) (Source)

    Kommentar


    • #3
      Der Unterschied zu A ist doch nur, dass vorher gefragt wird ob es ein array ist. Wenn ich das nicht tue erscheint diese Warnung.

      Warning: Invalid argument supplied for foreach() in
      Deshalb die Abfrage. Wie werde ich diese Warnung denn anders los?

      Kommentar


      • #4
        der unterschied ist hier
        PHP-Code:
        $menu=array(
        "Home"=>array("root"=>$pfad."index.php"),
        "A"=>array("root"=>$pfad."a.php",
            
        "A 1"=>$pfad."a1.php",
            
        "A 2"=>$pfad."a2.php"),
        "B"=>array("root"=>$pfad."b.php",
                            
        "B 1"=>!!!array!!!("root"=>"b1.php""B 2"=>$pfad."b2.php")),
        "C"=>array("root"=>$pfad."c.php")
        ); 
        ist das array an der stelle wirklich gewollt?

        Kommentar


        • #5
          Der Unterschied zu A ist doch nur, dass vorher gefragt wird ob es ein array ist
          Nein der Unterschied zu A ist, dass die Unterpunkte von A (A1 und A2) eben selber keine Arrays sind. Bei B ist der Unterpunkt B1 ein Array!
          Gutes Tutorial | PHP Manual | MySql Manual | PHP FAQ | Apache | Suchfunktion für eigene Seiten

          [color=red]"An error does not become truth by reason of multiplied propagation, nor does truth become error because nobody sees it."[/color]
          Mohandas Karamchand Gandhi (Mahatma Gandhi) (Source)

          Kommentar


          • #6
            Mein Array/Menu war nicht ganz korrekt. So sollte es aussehen.
            A
            - A 1
            - A 2
            B
            - B 1
            - - B 1.1
            - - B 1.2
            C
            Warum diese Meldung nur beim der dritten Schleife erscheint verstehe ich nicht.

            Kommentar


            • #7
              ist trotzdem kein Wunder, dass array ausgegeben wird
              PHP-Code:
              foreach($value as $key2=>$value2){
                              if(
              $key2 != "root")
                                  echo 
              " - <a href=".$value2.">$key2</a><br>\n";
                              } 
              wird das Array für B1 ausgegeben, wenn du hier noch eine Ebene tiefer willst musst du schon hier die nächste Schleife ansetzen

              Kommentar


              • #8
                PHP-Code:
                ...
                            foreach(
                $value as $key2=>$value2){
                                if(
                $key2 != "root")
                                    echo 
                " - <a href=".$value2.">$key2</a><br>\n";
                                }

                                        foreach(
                $value2 as $key3=>$value3){
                ... 
                Direkt dadrunter beginnt doch die nächste Schleife ...

                Kommentar


                • #9
                  Problem ist aber, dass $value2 an der Stelle wo du es ausgibst in dem Fall von B1 ein Array ist weshalb auch einfach nur array ausgegeben wird. Was du bräuchtest wäre eher
                  PHP-Code:
                  foreach($value as $key2=>$value2){
                    if(
                  $key2 != "root" AND !is_array($value2)) {
                     echo 
                  " - <a href=".$value2.">$key2</a><br>\n";
                    }elseif(
                  $is_array($value2)) {
                      foreach(...) {...}
                    }

                  Kommentar


                  • #10
                    Das war sehr gut!

                    Es klappt schon fast.
                    PHP-Code:
                    $menu=array(
                    "Home"=>array("root"=>$pfad."index.php"),
                    "A"=>array("root"=>$pfad."a.php",  "A 1"=>$pfad."a1.php","A 2"=>$pfad."a2.php",  "A 3"=>$pfad."a3.php"),
                    "B"=>array("root"=>$pfad."b.php",  "B 1"=>$pfad."b1.php",

                                        
                    "B 1.1"=>array("root"=>"b1.1.php""B 1.2"=>$pfad."b1.2.php")),
                    "C"=>array("root"=>$pfad."c.php")
                    );


                      foreach(
                    $menu as $key=>$value){
                           echo 
                    "<a href=".$value['root'].">$key</a><br>";

                                foreach(
                    $value as $key2=>$value2){
                                    if(
                    $key2 != "root" && !is_array($value2)){
                                        echo 
                    " - <a href=".$value2.">$key2</a><br>\n";
                                      }elseif(
                    is_array($value2)) {

                                            foreach(
                    $value2 as $key3=>$value3){
                                                if(
                    $key3 != "root"  ){
                                                    echo 
                    " - - <a href=".$value3.">$key3</a><br>\n";
                                                }
                                            }

                                            }

                                  }


                        }
                    echo 
                    "<pre>";
                     
                    print_r($menu);
                    echo(
                    "</pre>");
                    ?>
                    Der Link ist nun richtig, nur erscheint kein B 1.2. 
                    PHP-Code:
                    A
                    A 1
                    A 2
                    A 3
                    B
                    B 1
                    - - B 1.2
                    (- - B 1.2)

                    Zuletzt geändert von janein; 10.04.2008, 18:43.

                    Kommentar

                    Lädt...
                    X