Multi-Dimensional Arrays Help - And Other Questions on Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    #31
    Originally posted by fjm
    You're right. In every day practice, I couldn't see anything like that; thank God. I think that the most I could see would be a 2D or maybe a 3D array but I wouldn't think that I would have one over that.

    Maybe a better example would be a 2D array.

    Code:
    $result = array
    (
      array
      (
        'mykey0' => 'myvalue0',
        'mykey1' => 'myvalue1',
        'mykey2' => 'myvalue2'
      ),
      array
      (
        '0' => 'somthin',
        '1' => 'somthin',
        '2' => 'somthin',
        '3' => 'somthin else'
      )
    )
    Say that I wanted to get the value "somthin else".... Could I do..

    Code:
    foreach($result as $key => $val)
    {
      echo $key['3'][$val];
    }
    Would this work?
    not like that: more like this.
    Code:
    foreach($result as $key => $val)
    {
       if($val[3]){                 //very important like i said before
          echo $var[3];
       }
    }
    or like dormilich posted (to much posters, can't keep up)

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #32
      Originally posted by fjm
      To me.. If I can master getting data back out of this monster, I will be very happy in my newly found knowledge of arrays.
      well, looks like a "simple" array representation of a html/DOM table… *g*

      Comment

      • fjm
        Contributor
        • May 2007
        • 348

        #33
        Originally posted by Markus
        With nested foreach loops.

        Code:
        foreach ( $array as $inner )
        {
        	foreach ( $inner as $key => $val )
        	{
        		if ( is_array($val) )
        		{
        			foreach( $val as $key => $val2 )
        			{
        				echo "{$key} => {$val2}<br />";
        			}
        		}
        		else
        		{
        			echo "{$key} => {$val}<br />";
        		}
        	}
        }
        Sometimes hardcoding the loops will be out-of-the-question, so you'll need to a look see at recursion.

        - mark.
        Hi Mark. Thank you very much for that example and the link on recursion. I am skimming over it now. I wouldn't think that I would need anything over a 3D array but you never know.

        Question: I noticed that you have 2 or 3 foreach loops in your example. Would it be safe to say that I need a foreach loop for each array?

        Comment

        • fjm
          Contributor
          • May 2007
          • 348

          #34
          Originally posted by Ciary
          not like that: more like this.
          Code:
          foreach($result as $key => $val)
          {
             if($val[3]){                 //very important like i said before
                echo $var[3];
             }
          }
          or like dormilich posted (to much posters, can't keep up)
          Ahha.. OK, I did miss that in a prior post. Thanks for bringing that to my attention again. So I need to test for the key first.. If its there then echo it.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #35
            Originally posted by Ciary
            (to much posters, can't keep up)
            there's hardly any time to breath inbetween…

            Comment

            • fjm
              Contributor
              • May 2007
              • 348

              #36
              Originally posted by Dormilich
              well, looks like a "simple" array representation of a html/DOM table… *g*
              That's exactly what it is. :) Only problem was, at that time, I totally didn't get arrays and anything over an associative array sent me running in the other direction. I don't know why but arrays were freaky things for me. I mean, they are really not that bad now that you guys have taken the time to explain them to me.

              I feel that I still do need some help though in learning how to gall the data back out of an array.

              Comment

              • fjm
                Contributor
                • May 2007
                • 348

                #37
                Originally posted by Dormilich
                there's hardly any time to breath inbetween…
                Its so fun though. :P

                Comment

                • Ciary
                  Recognized Expert New Member
                  • Apr 2009
                  • 247

                  #38
                  if you need multiple elements from each array, you do need 1 foreach for each array. except if it contains multiple element types. then, you will probably know what it contains and how large it is.

                  in that case use $Arr['name'] or something like that.

                  EDIT nearly 40 posts on this threat. lets post till we reach 50 :)

                  Comment

                  • fjm
                    Contributor
                    • May 2007
                    • 348

                    #39
                    Originally posted by Ciary
                    if you need multiple elements from each array, you do need 1 foreach for each array. except if it contains multiple element types. then, you will probably know what it contains and how large it is.

                    in that case use $Arr['name'] or something like that.

                    EDIT nearly 40 posts on this threat. lets post till we reach 50 :)
                    lol... OK, I'm down... I have no other life anyway. My wife hates me and the cat won't come to anymore so its just me and the good ol' puter. :)

                    Can you explain what you mean about "if I need more elements"? I think you mean if I need more values from the array?

                    Comment

                    • fjm
                      Contributor
                      • May 2007
                      • 348

                      #40
                      Would you guys mind going over Atli's array? I wouldn't mind using it as an example. Even with all the help you guys are giving me, I am still a little freaked out by this code. How would I even get my data into it?

                      There are 11 arrays in there all together.

                      Comment

                      • Ciary
                        Recognized Expert New Member
                        • Apr 2009
                        • 247

                        #41
                        Originally posted by fjm
                        lol... OK, I'm down... I have no other life anyway. My wife hates me and the cat won't come to anymore so its just me and the good ol' puter. :)

                        Can you explain what you mean about "if I need more elements"? I think you mean if I need more values from the array?
                        exacly :) but you mostly use foreach if all values are the same type (like arrays) and you dont know the boundaries of your array.

                        otherwise you better use this
                        Code:
                        foreach($result as $val){
                        echo $val['name'].": ".$val['points']['math']
                        }
                        this will return the math results for every student

                        i hope this clearifies things

                        and i'm sorry about your cat disliking you :( :p

                        Comment

                        • fjm
                          Contributor
                          • May 2007
                          • 348

                          #42
                          Thanks Ciary, Yes, it does help. Your saying that all the values need to be inside of an array. (At least I think that's what you are saying).

                          Yeah, the cat just wants attention and gets mad when I am busy and can't play. She just plops herself in front of me and now I got loads of cat fur stuck to the monitor. :)

                          Comment

                          • Ciary
                            Recognized Expert New Member
                            • Apr 2009
                            • 247

                            #43
                            Code:
                               1. $pages = array(
                               2.     array(
                               3.         "Title" => "First page",
                               4.         "Tables" => array(
                               5.             array(
                               6.                 "Title" => "First table",
                               7.                 "Rows" => array(
                               8.                     array(
                               9.                         "Cells" => array("Col1", "Col2", "Col3")
                              10.                     ),
                              11.                 )
                              12.             ),
                              13.             array(
                              14.                 "Title" => "Second table",
                              15.                 "Rows" => array(
                              16.                     array(
                              17.                         "Cells" => array("Col1", "Col2", "Col3")
                              18.                     )
                              19.                 )
                              20.             )
                              21.         )
                              22.     )
                              23. );
                            first, he has an associative array $pages. in it is an array which always contains 2 elements: a title and an array of tables.

                            the array tables will also contains another array with 2 element (this happens a lot). one with the title and one with the rows of that table. each row in rows is also an array containing the cells on that row..

                            i think thats about it. hope you understand it now. it's only a 7D associative array :)

                            Comment

                            • fjm
                              Contributor
                              • May 2007
                              • 348

                              #44
                              OK, so taking Atli's array..

                              Code:
                              $pages = array
                              (
                                array
                                (
                                  "Title" => "First page",
                                  "Tables" => array
                                  (
                                    array
                                    (
                                      "Title" => "First table",
                                      "Rows" => array
                                      (
                                        array
                                        (
                                          "Cells" => array
                                          (
                                            "Col1", "Col2", "Col3"
                                          )
                                        ),
                                      )
                                    ),
                                    array
                                    (
                                      "Title" => "Second table",
                                      "Rows" => array
                                      (
                                        array
                                        (
                                          "Cells" => array
                                          (
                                            "Col1", "Col2", "Col3"
                                          )
                                        )
                                      )
                                    )
                                  )
                                )
                              );
                              Here is the way I am reading this. Please correct me if I am wrong. I count a total of 11 arrays in this code. Can we say that this is an 11 dimension array?

                              I see that the outermost array holds all others. The next one in holds the tables. The next one in holds the actual table data. The next one holds the rows. The next one holds the actual row data. The next one holds the cells and the last array holds the actual cell data. Am I correct???

                              Comment

                              • Dormilich
                                Recognized Expert Expert
                                • Aug 2008
                                • 8694

                                #45
                                Originally posted by Ciary
                                irst, he has an associative array $pages. in it is an array which always contains 2 elements: a title and an array of tables.
                                I need to butt in here, but
                                the first array is numeric and holds the "pages", the second array is associative and holds the info about the actual page (i.e. title and table)

                                Comment

                                Working...