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:
Code:array ( array ( 'mykey0' => 'myvalue0', 'mykey1' => 'myvalue1', 'mykey2' => 'myvalue2' ), array ( '0' => 'somthin', '1' => 'somthin', '2' => 'somthin', array('0' =>'somethin here') ) )
Comment
-
Please tell me that I wouldn't ever encounter an array like this. :)Comment
-
call it MD or nested array.
which one? you have a total of 4 arrays…
Code:$array $array[0] $array[1] $array[1][0]
Code:array('0' => "value", "value")
Comment
-
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.
Code:$result = array ( array ( 'mykey0' => 'myvalue0', 'mykey1' => 'myvalue1', 'mykey2' => 'myvalue2' ), array ( '0' => 'somthin', '1' => 'somthin', '2' => 'somthin', '3' => 'somthin else' ) )
Code:foreach($result as $key => $val) { echo $key['3'][$val]; }
Comment
-
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.
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.
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