Multi-Dimensional Arrays Help - And Other Questions on Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #16
    Originally posted by fjm
    Kind of like sayins that each cell can have an array inside of it and it just keeps going from there.
    that's the crucial point.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #17
      Originally posted by fjm
      If I wanted to have a strict set of rules, would I just use E_ALL?
      yup.

      (blablablubb…20 character limit exeeded…narf)

      Comment

      • Ciary
        Recognized Expert New Member
        • Apr 2009
        • 247

        #18
        take a look at this: its a visual example of a 4D array. hope this helps.

        and indeed you create it by putting an array in a array. and that one in another array and that one in another and ... (you get the poin :) )

        Comment

        • fjm
          Contributor
          • May 2007
          • 348

          #19
          Originally posted by Dormilich
          that's the crucial point.
          Scary thought because each cell or array can have an infinite number of arrays. It blows my mind to even imagine it.

          Ok, so if I can just bring this into perspective and maybe have you guys guide me a bit now that I can visualize it. Let's say I have an array like this:

          Code:
          array
          (
            array
            (
              'mykey0' => 'myvalue0',
              'mykey1' => 'myvalue1',
              'mykey2' => 'myvalue2'
            ),
            array
            (
              '0' => 'somthin',
              '1' => 'somthin',
              '2' => 'somthin',
              array('0' =>'somethin here')
            )
          )
          OK. I have a MD array here with a nested array. (Not sure what to call it) How would I refer to the nested array with a foreach?

          Comment

          • fjm
            Contributor
            • May 2007
            • 348

            #20
            Originally posted by Ciary
            take a look at this: its a visual example of a 4D array. hope this helps.

            and indeed you create it by putting an array in a array. and that one in another array and that one in another and ... (you get the poin :) )
            Great example Ciary, thanks. Is this a 4D array because each box has 4 columns or because each box has 4 columns? How did you determine that it was 4?

            Please tell me that I wouldn't ever encounter an array like this. :)

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #21
              Originally posted by fjm
              Scary thought because each cell or array can have an infinite number of arrays. It blows my mind to even imagine it.
              you haven't seen where such array are used in reality…
              Originally posted by fjm
              OK. I have a MD array here with a nested array. (Not sure what to call it)
              call it MD or nested array.
              Originally posted by fjm
              How would I refer to the nested array with a foreach?
              which one? you have a total of 4 arrays…
              Code:
              $array
              $array[0]
              $array[1]
              $array[1][0]
              although you might run into problems here:
              Code:
              array('0' => "value", "value")
              because array(0 => x) and array(x) is the same and PHP is not strongly typed (i.e. it can easily switch variable types and the difference between 0 and '0' is not much)

              Comment

              • fjm
                Contributor
                • May 2007
                • 348

                #22
                Code:
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant name - assumed 'name' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 4
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant occupation - assumed 'occupation' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 5
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant age - assumed 'age' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 6
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant specialty - assumed 'specialty' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 7
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant name - assumed 'name' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 11
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant occupation - assumed 'occupation' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 12
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant age - assumed 'age' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 13
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant specialty - assumed 'specialty' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 14
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant name - assumed 'name' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 18
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant occupation - assumed 'occupation' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 19
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant age - assumed 'age' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 20
                [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant specialty - assumed 'specialty' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 21
                hehe.. Thanks Dormilich, my errors are now back!

                Comment

                • Ciary
                  Recognized Expert New Member
                  • Apr 2009
                  • 247

                  #23
                  Code:
                  foreach($array as $val){
                     if($val[3]){
                        echo $val[3][0];    //this will print 'somethin here'
                     }
                  }
                  the 'if' is very important otherwise you will get an error because you try to echo something that doesnt exist.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #24
                    Originally posted by fjm
                    Great example Ciary, thanks. Is this a 4D array because each box has 4 columns or because each box has 4 columns? How did you determine that it was 4?
                    the 4x5 (5x4) tables are 2D, then you have 3 rows making it 3D and 4 columns making 4D.

                    Comment

                    • Ciary
                      Recognized Expert New Member
                      • Apr 2009
                      • 247

                      #25
                      it has nothing to do with row/column count. it's purely the number of dimentions. or better: the dept of the nested array.
                      Code:
                      $array = array(array(array(array())))
                      this is a 4D array

                      Comment

                      • fjm
                        Contributor
                        • May 2007
                        • 348

                        #26
                        Originally posted by Dormilich
                        you haven't seen where such array are used in reality…
                        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?

                        Comment

                        • fjm
                          Contributor
                          • May 2007
                          • 348

                          #27
                          Originally posted by Ciary
                          it has nothing to do with row/column count. it's purely the number of dimentions. or better: the dept of the nested array.
                          Code:
                          $array = array(array(array(array())))
                          this is a 4D array
                          OK, thanks for that Ciary.. So it simply the number of "array"(s) in the statement. Yes? You say dimensions and I am thinking code block. These fancy terms just screw me up sometimes. :)

                          Atli once helped me and gave me an array to do something that I needed. "WILD" stuff, man.. It was 6 or 7 arrays (dimensions). I flipped out just looking at it.

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #28
                            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.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #29
                              Originally posted by fjm
                              Would this work?
                              no, $key is either string or integer (read here).

                              use
                              Code:
                              $result[1]['3']

                              Comment

                              • fjm
                                Contributor
                                • May 2007
                                • 348

                                #30
                                I found it! Here is the array that Atli gave me that just flipped me out.

                                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")
                                                    )
                                                )
                                            )
                                        )
                                    )
                                );
                                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.

                                Comment

                                Working...