Ageneric Posted February 16, 2010 Share Posted February 16, 2010 Hello all, I am new to HTML/PHP so any help would be great. I have a multidimensional array filled with values that I am trying to output into an html table. This works (outputs value of array): echo $table[0][0]; This does not work (outputs "Array[0]" instead of the actual value within the table): echo "<table border = 1>\n"; echo "<td>Col 1</td><td>Col 2</td><td>Col 3</td>"; echo "</tr>\n"; echo "<td>$table[0][0]</td><td>$table[0][0]</td><td>$table[0][0]</td>"; echo "</table>\n"; I was going to use a couple of loops to output the array into the table however the way I have above is not working and really screwing me up. Does anyone have a solution? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/ Share on other sites More sharing options...
idontkno Posted February 16, 2010 Share Posted February 16, 2010 Try this: echo "<table border = 1>\n"; echo "<td>Col 1</td><td>Col 2</td><td>Col 3</td>"; echo "</tr>\n"; echo "<td>".$table[0][0]."</td><td>".$table[0][0]."</td><td>".$table[0][0]."</td>"; echo "</table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012939 Share on other sites More sharing options...
teamatomic Posted February 16, 2010 Share Posted February 16, 2010 Something like: $j = count($array); $i=1; echo '<table><tr>'; for($i;$i<=$j'$i++) { echo "<td>Col $i</td>"; } echo "</tr><tr>"; foreach ($array as $sub) { $i=1; foreach($sub as $item) { echo "<td>$item</td>"; if ($i%$j==0){echo "</tr><tr>";} } $i++; } HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012940 Share on other sites More sharing options...
Ageneric Posted February 16, 2010 Author Share Posted February 16, 2010 Thank you both very much. Both posts helped a lot. Now I just have to mess with some loops and get the output just right. Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012941 Share on other sites More sharing options...
Psycho Posted February 16, 2010 Share Posted February 16, 2010 Personally, I perfer to include my variables within the quoted string. The problem you were having is caused by how PHP interprets the multidimensional array in the string. You can overcome that by enclosing the variable within curly braces. I typically always enclose my variables in that manner. Example: echo "<table border=\"1\">\n"; echo "<tr>\n"; echo "<td>Col 1</td><td>Col 2</td><td>Col 3</td>"; echo "</tr>\n"; echo "<tr>\n"; echo "<td>{$table[0][0]}</td><td>{$table[0][0]}</td><td>{$table[0][0]}</td>"; echo "</tr>\n"; echo "</table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012944 Share on other sites More sharing options...
Ageneric Posted February 16, 2010 Author Share Posted February 16, 2010 Personally, I perfer to include my variables within the quoted string. The problem you were having is caused by how PHP interprets the multidimensional array in the string. You can overcome that by enclosing the variable within curly braces. I typically always enclose my variables in that manner. Thanks, I figure once I get more practice with html/php I will learn these little "tricks" that help me organize and make my code more neat. One more quick question. In PHP, is there any way to swap the contents in a multidimensional array? For example, I want all of the values in Array[$i][1] to be swapped with the values in Array[$i][2]. This is because I am trying to output the values in a certain order and the way they are ordered in the array is not how I want to output them (therefore when I output them in a loop it is in the wrong order). Thanks Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012952 Share on other sites More sharing options...
teamatomic Posted February 16, 2010 Share Posted February 16, 2010 The only use one loop and do something like: echo "<td>{$sub[3]}</td><td>{$sub[0]}</td><td>{$sub[2]}</td><td>{$sub[1]}</td>"; HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012958 Share on other sites More sharing options...
Ageneric Posted February 16, 2010 Author Share Posted February 16, 2010 The only use one loop and do something like: echo "<td>{$sub[3]}</td><td>{$sub[0]}</td><td>{$sub[2]}</td><td>{$sub[1]}</td>"; HTH Teamatomic Im not so sure I know exactly what you mean. Let me provide some of my code ($counter is a variable that holds a value and increments so you can ignore it for the purposes of this question). for($i = 0; $i < 5; $i++) { for($j = 0; $j <= 2; $j++) { echo "<td>".$table[$counter][$j]."</td>"; } That is how the loops are structured. I would like the outputs when $j = 1 and $j = 2 to be switched? If there isnt an easy way than dont worry about it. By the way, it seems you're a skier, where do you ski at? Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012963 Share on other sites More sharing options...
idontkno Posted February 16, 2010 Share Posted February 16, 2010 The only use one loop and do something like: echo "<td>{$sub[3]}</td><td>{$sub[0]}</td><td>{$sub[2]}</td><td>{$sub[1]}</td>"; HTH Teamatomic Why not just use PHP's array_replace() function? Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012992 Share on other sites More sharing options...
Psycho Posted February 16, 2010 Share Posted February 16, 2010 You can create a custom function to order the array however you want using usort(). So, let's say you want to order the array by items at the index 2 in ascending order, and if those items are equal, then do a sub-sort on the items at index 4 in descending order: function customSort($a, $b) { if ($a[2] == $b[2]) { if ($a[4] == $b[4]) { return 0; } return ($a[4] < $b[4]) ? 1 : -1; } return ($a[2] > $b[2]) ? 1 : -1; } usort($myArray, 'customSort'); Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012996 Share on other sites More sharing options...
teamatomic Posted February 16, 2010 Share Posted February 16, 2010 Big Sky/Moonlight HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1012998 Share on other sites More sharing options...
Ageneric Posted February 17, 2010 Author Share Posted February 17, 2010 Why not just use PHP's array_replace() function? Interesting, would that work for a multidimensional array? Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1014025 Share on other sites More sharing options...
Psycho Posted February 18, 2010 Share Posted February 18, 2010 Why not just use PHP's array_replace() function? Interesting, would that work for a multidimensional array? How is array_replace() going to help you? That function is meant to "replace" values in one array with the values from another array. You have a single array that you want to output into an HTML table. All you need to do is perform a foreach() on the array and output the results accordingly. If you want to reorder a multi-dimensional array then use usort() as I stated. If you want more specific help then do a print_r() on your array and copy the results to a txt file and attach to this thread. Then provide what columns you want displayed and how they should be sorted. Quote Link to comment https://forums.phpfreaks.com/topic/192216-multidimensional-array-into-an-html-table-help/#findComment-1014428 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.