Multi-Dimensional Arrays Help - And Other Questions on Arrays
Collapse
X
-
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:
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?Code:array ( array ( 'mykey0' => 'myvalue0', 'mykey1' => 'myvalue1', 'mykey2' => 'myvalue2' ), array ( '0' => 'somthin', '1' => 'somthin', '2' => 'somthin', array('0' =>'somethin here') ) )Comment
-
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
-
you haven't seen where such array are used in reality…
call it MD or nested array.
which one? you have a total of 4 arrays…
although you might run into problems here:Code:$array $array[0] $array[1] $array[1][0]
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)Code:array('0' => "value", "value")Comment
-
hehe.. Thanks Dormilich, my errors are now back!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
Comment
-
Comment
-
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.
Say that I wanted to get the value "somthin else".... Could I do..Code:$result = array ( array ( 'mykey0' => 'myvalue0', 'mykey1' => 'myvalue1', 'mykey2' => 'myvalue2' ), array ( '0' => 'somthin', '1' => 'somthin', '2' => 'somthin', '3' => 'somthin else' ) )
Would this work?Code:foreach($result as $key => $val) { echo $key['3'][$val]; }Comment
-
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
-
With nested foreach loops.
Sometimes hardcoding the loops will be out-of-the-question, so you'll need to a look see at recursion.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 />"; } } }
- mark.Comment
-
I found it! Here is the array that Atli gave me that just flipped me out.
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.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") ) ) ) ) ) );Comment

Comment