<?php
for($i=0; $i<sizeof($top_name); $i++)
{
for($i=0; $i<sizeof($top_name); $i++)
{
echo "<tr><td>".$sub_name[$i]."</td><td>".$sub_diff[$i]."</td></tr>";
echo "<tr><td>".$top_name[$i]."</td><td>".$top_diff[$i]."</td> <td>".$top_size[$i]."</td></tr>";
}
}
?>
Error :
Notice: Undefined offset: 2 in C:\xampp\htdocs\TRY\data.php on line 28
Notice: Undefined offset: 2 in C:\xampp\htdocs\TRY\data.php on line 28
I'm trying to fetch arrays and arranging them in table but it is giving that error.
Please solve this. Thanks in advance...
Assuming you have these elements in $top_name
and $sub_name
array:
$top_name = array( 'First Name', 'Second Name', 'Third Name', 'Fourth Name' );
$sub_name = array( 'Sub One', 'Sub Two' );
your two loops acts in this way:
1) main loop > $i = 0 > executed
2) 2nd loop > $i = 0 > executed
3) 2nd loop > $i = 1 > executed
4) 2nd loop > $i = 2 > executed > undefined offset for $sub_name
5) 2nd loop > $i = 3 > executed > undefined offset for $sub_name
6) main loop > $i = 4 > not executed
So, the deeper loop is executed also for non-existent $sub_name
elements, whereas the main loop is executed only once.
In the second loop, you have to change incrementing variable name and condition variable:
for( $i=0; $i<sizeof( $top_name ); $i++ )
{
for( $n=0; $n<sizeof( $sub_name ); $n++ )
{
echo "<tr><td>".$sub_name[$n]."</td><td>".$sub_diff[$n]."</td></tr>";
echo "<tr><td>".$top_name[$i]."</td><td>".$top_diff[$i]."</td> <td>".$top_size[$i]."</td></tr>";
}
}