Loop through a PHP Multidimensional Array - php

Im trying to create a 2D array with a specific number of rows and cols which I have stored as the variable $n ; so if $n was 5, I would have 5 rows and 5 cols, all with random numbers. I have created a for loop (as shown below) that generates the correct amount of rows but I cannot figure out how to do the same with the columns at the same time. The code I have at the moment is shown below.
<?php
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
for($j=0; $j<$n; $j++) { // Runs the loop times $n
print "<tr>";
for($k=0; $k<$n; $k++) { // Runs the loop times $n
print "<td>" . $value[$j][$k] . "</td>";
}
print "</tr>";
}
print "</table>";
?>
Any help would be appreciated in learning to create this loop of the array. Thanks in advance.

You need a second loop inside the first loop:
for($i=0; $i<=$n; $i++) { // rows
for($j=0; $j<=$n; $j++) { // columns
$value[$i][$j] = rand(1,20);
}
}

Try this instead :
$n = 3;
for($i=0; $i<=$n; $i++) {
$value[$i][0] = rand(1,20);
$value[$i][1] = rand(1,20);
$value[$i][2] = rand(1,20);
$value[$i][3] = rand(1,20);
}
print "<table>";
foreach($value as $row){
print "<tr>";
foreach($row as $cell){
print "<td>" . $cell . "</td>";
}
print "</tr>";
}
print "</table>";
And using a for loop :
print "<table>";
for($i=0; $i<=$n; $i++) { // rows
print "<tr>";
for($j=0; $j<=(count($value[$i])-1); $j++) { // columns
echo "<td>".$value[$i][$j]."</td>";
}
print "</tr>";
}
print "</table>";

Related

How to break a column to fix a php table [duplicate]

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed last year.
I had a little problem, i cannot break the table because I'm looking for a 16 columns in these table to create a unicode table...
The aim is to find where i must break the line of the column as like as there:
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
enter image description here
You used the modulus in the first loop, so use it again in the second
print "</tr>";
print "<tr>";
for($i = 1; $i <= 50000; $i++){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>$i</td><td>$unicodeChar</td>\n";
if ( $i % $columnas/2 ) == 0 ) {
print "</tr><tr>\n";
}
}
print "</tr>\n";
Thank you very much for this help, i have been fixed!!
here is the code that i fixed, with your idea :)
´´´´
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
print "<tr>";
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
if (($i % 8 ) == 0 ){
print "</tr><tr>";
}
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
´´´´
Like Riggsfolly said but maybe divide the columns number by 2 before the modulo, because there are 2 TDs / iteration:
if ( $i % ($columnas/2) ) == 0 ) {
print "</tr><tr>\n";
}

PHP get min and max values from 50 random numbers?

<?php
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand (1, 200);
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Here is code but my question is how can I find max and min from values of the table? Do I have to make random numbers somehow into array?
You can do as following without changing much of your code
<?php
$array = array(); // <-- added code
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand(1, 200);
$array[] = $rand; // <-- added code
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
$min = min($array); // <--- added code
$max = max($array); // <--- added code
?>
Check it out the max and min value of an aray in PHP using library function.
for($col = 0; $col < 5; $col ++) {
$rand[] = rand (1, 200);
}
echo $max = max($rand)."<br/>";
echo $min = min($rand)."<br/>";
print_r($rand);

display first set of result in row

I want to iterate an array of data using foreach loop.
However, I want the first 4 results to display two items per row.Thereafter I want the rest of the data to display I item per row
i found a possible solution here
$data = range(1, 30);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 2; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 2; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
how the problem with this code is that it displays all the data in rows of two.i however only want the first 4 results to display that way.
You can do something like this:
$data = range(1, 30);
for($count = 0; $count < count($data); ++$count){
if($count < 4){
if($count % 2 == 0){
echo "<tr><td>" . $data[$count] . "</td>";
}else{
echo "<td>" . $data[$count] . "</td></tr>";
}
}else{
echo "<tr><td>" . $data[$count] . "</td></tr>";
}
}
Here's the demo
You're using the already incremented $i variable twice.
$i is never < 2
Add this instead
for($j = 0; $j < 2; $j++) {
echo "\t<td>-</td>\n";
}
Test here
Try with -
$data = range(1, 30);
for($count = 0; $count < count($data); $count++)
{
echo "<tr>";
if($count < 2) {
for($i = 1; $i <= 2; $i++) {
echo "<td>" . $data[$count] . "</td>";
if($i == 1) {
echo "</tr><tr>";
}
}
} else {
echo "<td>" . $data[$count] . "</td>";
}
echo "</tr>";
}
What about something like this?
echo '<table border=1>';
$data = range(1, 30);
for($count = 0; $count < count($data); $count++){
echo "<tr>\n";
if($count < 4) {
echo "\t<td>$data[$count]</td>\n";
$count++;
echo "\t<td>$data[$count]</td>\n";
} else {
echo "\t<td colspan=2>$data[$count]</td>\n";
}
echo "</tr>\n";
}
echo '</table>';

php array with in array names don't output

Is this possible to do?
i'm looking to build a grid of values, but I was wondering if this is possible
$section_array = array(
$array_1[$i],
$array_2[$i],
$array_3[$i]
);
for ($i = 1; $i <= 31; $i++) {
echo "<tr>";
for ($j = 0; $j < 3; $j++) {
echo "<td>" . $section_array[$j] . "</td>"; // 3
}
echo "</tr>";
}
This is basically the idea, I want to print out 1 value of each section (array_1, array_2) etc
then move to the next day, and print out the values of day 2 and so on. is it possible without having to just list every single one in the 2nd for loop?
Are you looking for something like this? Loop through your array of arrays and print 2 values from the sub array.
$arr = //insert data here
foreach($arr as $subarr) {
echo '<tr><td>' . $subarr['myval1'] . '</td><td>' . $subarr['myval2'] . '</td></tr>';
}
$secname_array = array($val1, $val2, $val3);
$daycount = 31;
for ($i = 1; $i <= $daycount; $i++){
echo"<tr>
<td>$i</td>";
foreach ($secname_array as $v)
{
echo "<td>". $v[$j]. "</td>";
}
$j++;
echo"</tr>";
}
?>

Creating a multiplication "grid"

I am trying to create a simple multiplication grid in PHP
It should be of the format for example for a 2x2 grid:
0 1 2
1 1 2
2 2 4
My issue is getting it to start from 0.
This is my nested for loop so far:
for($i=0;$i<=$_POST['rows'];$i++)
{
echo "<tr>";
for($j=0;$j<=$_POST['columns'];$j++)
{
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
}
echo "</tr>";
}
But it gives the output:
0 1 2
0 1 2
0 2 4
I need the column of 0's to be appropriate.
The way you're getting the top row of 0 1 2 3 is by that special-case on the X-axis. Do a similar special-case for the Y-axis ($j):
if ($i == 0) {
... 1 * $j ...
}
else if ($j == 0) {
... $i * 1 ...
}
else {
... $i * $j ...
}
You not only have $i==0 as special case but also $j==0:
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
elseif($j==0)
{
echo "<td>" . $i*1 . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
I don't understand the way you construct your grid. All you need is a row indicator and the number for the multiplication not a nested loop. Second: Why don't you start with 1 instead of catching the case inside the loop. This would be my variant of the multiplication “grid”
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
for( $i=1; $i <= $rows; $i++) {
$mult = $i * $number;
echo "<tr>
<td>" . $i.'*'. $j . "</td>
<td>".$mult."</td>
</tr>";
}
?>
This would to a simple grid (x * y) = result. If you want a complete multiplication table it would be something like this:
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
echo "<tr><th></th>";
for( $j=1; $j <= $number; $j++) {
echo "<th>".$j."</th>";
}
echo "</tr>";
for( $i=1; $i <= $rows; $i++) {
echo "<tr>";
echo "<th>".$i."</th>";
for( $j=1; $j <= $number; $j++) {
$mult = $i * $j;
echo "<td>".$mult."</td>";
}
echo "</tr>";
}
?>

Categories