Jump to content

Trying (and failing) To Print Out a Two-Dimensional Array


EricTehCleric

Recommended Posts

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 :D

 

[attachment deleted by admin]

Link to comment
Share on other sites

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 :D

 

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
Share on other sites

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

 

outputo.jpg

 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.