0

let's say I have these two arrays:

$array1 = array(1, 2, 3, 4, 5);
$array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);

As you can see, my arrays have different lengths. What I'm trying to do is to input these array values into an HTML table with the first column containing the values coming from $array1 and the second column containing the values coming from $array2. So, in this case right here, I should have a table of 10 rows (because $array2 contains 10 elements) and 2 columns (because I have 2 arrays). Also, I cannot know in advance which array is going to have more elements than the other (so, $array1 could be bigger than $array2, they could also have equal sizes). So, depending on which array has more elements, the number of rows in my table should adjust accordingly.

Any idea please?

Thank you

2 Answers 2

1
$array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
$array1 = array(1, 2, 3, 4, 5);
$a=count($array1);
$b=count($array2);
echo "<table border=1><tr><th>Array1</th><th>Array2</th></tr>";
if($a > $b)
{
    for($i=0;$i<$a;$i++)
    {
        echo "<tr><td>".$array1[$i]."</td>";
        echo "<td>".$array2[$i]."</td></tr>";
    }
}
if($b > $a)
{
    for($i=0;$i<$b;$i++)
    {
        echo "<tr><td>".$array1[$i]."</td>";
        echo "<td>".$array2[$i]."</td></tr>";
    }
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

1 Comment

I get undefined offset error, but I can correct that, thank you
0

Try thinking of some thing like below

this will give you atleast an idea of how to iterate them.

$array = array($array1,$arry2);

for($i = 0; $i < $array.length; $i++)
{

   $rows = $array[$i];

    for($j=0; $j< $rows.length; $rows++){
   }

}

i hope you can figure out the logic required from this . in case it doesnt work out post comment and we will walk through

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.