EricTehCleric Posted March 18, 2010 Share Posted March 18, 2010 I am writing a PHP table that contains the conjugations for 6 Spanish verbs. It uses a two dimensional array, and it *almost* works. $conjugations = array( array("hablar", "hablo", "hablas", "habla", "hablamos", "habláis", "hablan"), array("estudiar", "estudio", "estudias", "estudia", "estudiamos", "estudiáis", "estudian"), array("cantar", "canto", "cantas", "canta", "cantamos", "cantáis", "cantan"), array("nadar", "nado", "nadas", "nada", "nadamos", "nadáis", "nadan"), array("tocar", "toco", "tocas", "toca", "tocamos", "tocáis", "tocan"), array("dibujar", "dibujo", "dibujas", "dibuja", "dibujamos", "dibujáis", "dibujan") ); $header = array(" ","yo", "tú", "él / ella / ud.", "nosotros", "vosotros", "ellos / ellas / uds."); echo "<div>\n"; echo "\t<table border=\"1\">\n"; echo "\t\t<tr>\n"; foreach($header as $key => $value){ echo "<td>" . $value . "</td>\n"; }// end foreach header echo "\t\t</tr>\n"; ////////////////////////////////////////////////////////////////////////////////////// foreach($conjugations as $row => $rowValue){ echo "\t\t<tr>\n"; foreach($conjugations as $col => $colValue){ echo "\t\t\t<td>" . $conjugations[$row][$col] . "</td>\n"; }// loop through the conjugations of one of the nested arrays echo "\t\t</tr>\n"; }// end looping /////////////////////////////////////////////////////////////////////////////////////// echo "\t</table>\n"; echo "</div>"; ?> If I can get some help on this I would appreciate it very much. Sorry for my idiocy, I am just starting PHP. If my problem can be fixed quickly I will be sure to come back and help whoever I can [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/195725-trying-and-failing-to-print-out-a-two-dimensional-array/ Share on other sites More sharing options...
jamesxg1 Posted March 18, 2010 Share Posted March 18, 2010 I am writing a PHP table that contains the conjugations for 6 Spanish verbs. It uses a two dimensional array, and it *almost* works. $conjugations = array( array("hablar", "hablo", "hablas", "habla", "hablamos", "habláis", "hablan"), array("estudiar", "estudio", "estudias", "estudia", "estudiamos", "estudiáis", "estudian"), array("cantar", "canto", "cantas", "canta", "cantamos", "cantáis", "cantan"), array("nadar", "nado", "nadas", "nada", "nadamos", "nadáis", "nadan"), array("tocar", "toco", "tocas", "toca", "tocamos", "tocáis", "tocan"), array("dibujar", "dibujo", "dibujas", "dibuja", "dibujamos", "dibujáis", "dibujan") ); $header = array(" ","yo", "tú", "él / ella / ud.", "nosotros", "vosotros", "ellos / ellas / uds."); echo "<div>\n"; echo "\t<table border=\"1\">\n"; echo "\t\t<tr>\n"; foreach($header as $key => $value){ echo "<td>" . $value . "</td>\n"; }// end foreach header echo "\t\t</tr>\n"; ////////////////////////////////////////////////////////////////////////////////////// foreach($conjugations as $row => $rowValue){ echo "\t\t<tr>\n"; foreach($conjugations as $col => $colValue){ echo "\t\t\t<td>" . $conjugations[$row][$col] . "</td>\n"; }// loop through the conjugations of one of the nested arrays echo "\t\t</tr>\n"; }// end looping /////////////////////////////////////////////////////////////////////////////////////// echo "\t</table>\n"; echo "</div>"; ?> If I can get some help on this I would appreciate it very much. Sorry for my idiocy, I am just starting PHP. If my problem can be fixed quickly I will be sure to come back and help whoever I can Maybe try, $conjugations = array(); $conjugations[] = array("hablar", "hablo", "hablas", "habla", "hablamos", "habláis", "hablan"); $conjugations[] = array("estudiar", "estudio", "estudias", "estudia", "estudiamos", "estudiáis", "estudian"); $conjugations[] = array("cantar", "canto", "cantas", "canta", "cantamos", "cantáis", "cantan"); $conjugations[] = array("nadar", "nado", "nadas", "nada", "nadamos", "nadáis", "nadan"); $conjugations[] = array("tocar", "toco", "tocas", "toca", "tocamos", "tocáis", "tocan"); $conjugations[] = array("dibujar", "dibujo", "dibujas", "dibuja", "dibujamos", "dibujáis", "dibujan"); print_r($conjugations); It echo's Array ( [0] => Array ( [0] => hablar [1] => hablo [2] => hablas [3] => habla [4] => hablamos [5] => habláis [6] => hablan ) [1] => Array ( [0] => estudiar [1] => estudio [2] => estudias [3] => estudia [4] => estudiamos [5] => estudiáis [6] => estudian ) [2] => Array ( [0] => cantar [1] => canto [2] => cantas [3] => canta [4] => cantamos [5] => cantáis [6] => cantan ) [3] => Array ( [0] => nadar [1] => nado [2] => nadas [3] => nada [4] => nadamos [5] => nadáis [6] => nadan ) [4] => Array ( [0] => tocar [1] => toco [2] => tocas [3] => toca [4] => tocamos [5] => tocáis [6] => tocan ) [5] => Array ( [0] => dibujar [1] => dibujo [2] => dibujas [3] => dibuja [4] => dibujamos [5] => dibujáis [6] => dibujan ) ) James. Link to comment https://forums.phpfreaks.com/topic/195725-trying-and-failing-to-print-out-a-two-dimensional-array/#findComment-1028250 Share on other sites More sharing options...
teamatomic Posted March 18, 2010 Share Posted March 18, 2010 foreach($conjugations as $row => $rowValue){//$rowValue is your array echo "\t\t<tr>\n"; foreach($rowValue as $col => $colValue){//$rowValue is your array echo "\t\t\t<td>" .$colValue. "</td>\n";//$colValue is the elements value HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/195725-trying-and-failing-to-print-out-a-two-dimensional-array/#findComment-1028257 Share on other sites More sharing options...
EricTehCleric Posted March 18, 2010 Author Share Posted March 18, 2010 I'm not sure how printing the array contents helps. It won't print correctly in the table. It skips over the last index for some reason. It won't print <td>hablan</td> <td>estudian</td>... etc I'm also confused by the red comments above. Here's an image that might make things more clear Link to comment https://forums.phpfreaks.com/topic/195725-trying-and-failing-to-print-out-a-two-dimensional-array/#findComment-1028269 Share on other sites More sharing options...
teamatomic Posted March 18, 2010 Share Posted March 18, 2010 Then RTM on arrays. Your looping logic is flawed. You use the key from the array to loop but you only have 6 keys and 7 elements to display. So obviously your last element is dropped. Forget using keys, you have no need for them with what you are doing. foreach($header as $hvalue){ echo "<td>" . $hvalue . "</td>\n"; }// end foreach header echo "\t\t</tr>\n"; foreach($conjugations as $array){ echo "\t\t<tr>\n"; foreach($array as $value){ echo "\t\t\t<td>" . $value . "</td>\n"; }// loop through the conjugations of one of the nested arrays }// end looping HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/195725-trying-and-failing-to-print-out-a-two-dimensional-array/#findComment-1028287 Share on other sites More sharing options...
EricTehCleric Posted March 18, 2010 Author Share Posted March 18, 2010 I thank you for your help teamatomic. Just to let you know, <sarcasm>telling a new user to read the manual is a great way to encourage membership and make people feel welcome.</sarcasm> I told you in my first post that I'm new at PHP. I'm working on a deadline, and I don't have time to read a manual. Manuals are designed in such a manner as not to give up their information without a fight. Link to comment https://forums.phpfreaks.com/topic/195725-trying-and-failing-to-print-out-a-two-dimensional-array/#findComment-1028306 Share on other sites More sharing options...
teamatomic Posted March 19, 2010 Share Posted March 19, 2010 You're welcome, and I'm sorry you feel that way but I dont happen feel that telling you to RTM was in any way rude or mean. When you get a solution and dont understand it then the thing to do is go and read the manual and scan examples three times. As for you being a novice and under the gun, you found time to fire off, as you put it, a sarcastic reply. My advice is to learn to relax, because its no use getting your undies in a bundle due to pressure from above. No matter if the pressure is unwarranted or warranted by an inability to say "no" instead of "no problem". Its you that must learn to relax, me, yesterday morning at daybreak I dropped off of a remote peak in yellowstone into hip deep fluff. Took us the rest of the day to get back to a road and on the way saw a sow and her cub just out of the den for spring. Now thats relaxing. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/195725-trying-and-failing-to-print-out-a-two-dimensional-array/#findComment-1028349 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.