Multi-Dimensional Arrays Help - And Other Questions on Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjm
    Contributor
    • May 2007
    • 348

    #61
    *bump* Accidentally posted when I wanted to edit. Sorry..

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #62
      Originally posted by fjm
      My thinking is that it is a 7D array so in order for me to get into the col array, wouldn't I need 7 foreach loops? That is my thinking anyhow. Am I wrong?
      you need 3 loops to get there*.
      Code:
      foreach($pages as $page)
      {
          foreach ($page['Tables'] as $table)
          {
              foreach ($table['Rows'] as $row)
              {
                  foreach ($row['Cells'] as $cell) { // see previous post }
              }
          }
      }
      * the cells loop, that is

      Comment

      • fjm
        Contributor
        • May 2007
        • 348

        #63
        Originally posted by Dormilich
        you need 3 loops to get there*.
        Code:
        foreach($pages as $page)
        {
            foreach ($page['Tables'] as $table)
            {
                foreach ($table['Rows'] as $row)
                {
                    foreach ($row['Cells'] as $cell) { // see previous post }
                }
            }
        }
        * the cells loop, that is
        OK, thanks. That actually helps me to understand better. Let me ask you this Dormilich.. You say it will take me 3 loops to get there but I count 4 loops. Was the 3 a typo?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #64
          you need three loops to reach the Cells loop (the fourth, which was posted first).

          Comment

          • fjm
            Contributor
            • May 2007
            • 348

            #65
            Ok. I think I understand. I know one thing.. that is that I fee much more comfortable being able to get my data out of a 2D array than before. I am going to have to study this a bit because honestly, it has me baffled a bit.

            How long would you say it will take me to get this down; to really learn it? I mean, I totally understand the concept but all of the different arrays has my head spinning. I can't make the mental connection. :)

            Comment

            • Ciary
              Recognized Expert New Member
              • Apr 2009
              • 247

              #66
              Code:
              foreach($pages as $page){
                echo "<h1>".$page['Title']."</h1><br/>";
                foreach($page['tables'] as $table){
                  echo "<h3>".$table['Title']."</h3><br/>";
                  echo "<table>";
                  foreach($table['rows] as $row){
                    echo "<tr>";
                    foreach($row as $cell){
                      echo "<td>".$cell."</td>";
                    }
                    echo "</tr>";
                  }
                  echo "</table>";
                }
              }
              this is the full code to add title etc.

              to understand it wont take long. just try keep practising

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #67
                Originally posted by fjm
                How long would you say it will take me to get this down; to really learn it? I mean, I totally understand the concept but all of the different arrays has my head spinning. I can't make the mental connection. :)
                practice, practice, practice and to and fro some sleep ;)

                how long this takes you I can't tell, a good abstraction ability will help shortening it… maybe don't rely too much on the table visualization (if it goes beyond 2D)

                Comment

                • fjm
                  Contributor
                  • May 2007
                  • 348

                  #68
                  I think I see something..

                  Code:
                  $pages = array  [B]<-- value[/B]
                  (
                    array  [B]<-- key[/B]
                    (
                      "Title" => "First page"
                      , "Tables" => array
                      (
                        'a'=>'b',
                        array [B]<-- value[/B]
                        (
                          "Title" => "First table"
                          , "Rows" => array
                          (
                            'c'=>'d',
                            array [B]<-- key[/B]
                            (
                              'e'=>'f',
                              "Cells" => array [B]<-- value[/B]
                              (
                                "Col1"
                                , "Col2"
                                , "Col3"
                              )
                            )
                          )
                        )
                      )
                    )
                  );
                  Is this right?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #69
                    @Ciary

                    there's no need to put breaks after the headings, you can set those layout stuff better with CSS.

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #70
                      Originally posted by fjm
                      Is this right?
                      add <-- key to lines 6, 12

                      Comment

                      • Ciary
                        Recognized Expert New Member
                        • Apr 2009
                        • 247

                        #71
                        i know, it's a habbit of me to always break after an echo.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #72
                          Originally posted by Ciary
                          i know, it's a habbit of me to always break after an echo.
                          I don't mean the line break in the source code, I mean the <br> element. (and if I got this wrong, you didn't <br> after every echo…)

                          Comment

                          • fjm
                            Contributor
                            • May 2007
                            • 348

                            #73
                            I truly think I understand this better. Thanks guys! Also Ciary, I used your code and it also worked as did Dormilich's code. What I am going to do now is to study this and make myself some new examples to use. I won't stop until I fully understand them.

                            Arrays are really cool and I am really beginning to see the benefits of using whenever I can.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #74
                              Originally posted by fjm
                              What I am going to do now is to study this and make myself some new examples to use. I won't stop until I fully understand them.
                              that's what I call professional attitude. you're always welcome.

                              Originally posted by fjm
                              Arrays are really cool and I am really beginning to see the benefits of using whenever I can.
                              wait ’til you see objects… ;)
                              Last edited by Dormilich; Apr 21 '09, 12:06 PM. Reason: keep smiling

                              Comment

                              • Ciary
                                Recognized Expert New Member
                                • Apr 2009
                                • 247

                                #75
                                your welcome :)
                                (.............. ...i feel brainwashed.... ..............) ^^

                                for the <br/> i'm learning not to but its stronger then myself. :)

                                Comment

                                Working...