I try to insert values from a database into a 2d array in php, but the inner for loop does not work after the 1st iteration of the outer loop. Here is the code
for ($i=0; $i<$recCount; $i++) {
$row=mysqli_fetch_array($result, MYSQLI_ASSOC);
$orderNo[$i] = $row['orderNo'];
$orderDate[$i] = $row['orderDate'];
$subTotal[$i] = $row['subTotal'];
$discount[$i] = $row['discount'];
$deliveryCharge[$i] = $row['deliveryCharge'];
$grandTotal[$i] = $row['grandTotal'];
$k = 0;
for ($j=0; $j<$recCount2; $j++){
$row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);
$orderNo2[$i][$j] = $row2['orderNo'];
echo "orderNo2[$i][$j]: " .$orderNo2[$i][$j]. " ";
if ($orderNo2[$i][$j] == $orderNo[$i]){
/*$subNo($i,$k) = $row2['subNo'];
$ItemNo($i,$k) = $row2['ItemNo'];
$Description($i,$k) = $row2['Description'];
$Qty($i,$k) = $row2['Qty'];
$Price($i,$k) = $row2['Price'];
$Amount($i,$k) = $row2['Amount'];*/
$k++;
}
}
$recCount = 3, $orderNo[$i] has the value of 1, 2, 3. $recCount2 = 15, the $orderNo2[$i][$j] should have five 1s, five 2s, and five 3s for each $i, but the echoed result of orderNo2 is
orderNo2[0][0]: 1 orderNo2[0][1]: 1 orderNo2[0][2]: 1 orderNo2[0][3]: 1 orderNo2[0][4]: 1 orderNo2[0][5]: 2 orderNo2[0][6]: 2 orderNo2[0][7]: 2 orderNo2[0][8]: 2 orderNo2[0][9]: 2 orderNo2[0][10]: 3 orderNo2[0][11]: 3 orderNo2[0][12]: 3 orderNo2[0][13]: 3 orderNo2[0][14]: 3 orderNo2[1][0]: orderNo2[1][1]: orderNo2[1][2]: orderNo2[1][3]: orderNo2[1][4]: orderNo2[1][5]: orderNo2[1][6]: orderNo2[1][7]: orderNo2[1][8]: orderNo2[1][9]: orderNo2[1][10]: orderNo2[1][11]: orderNo2[1][12]: orderNo2[1][13]: orderNo2[1][14]: orderNo2[2][0]:enter code here
orderNo2[2][1]: orderNo2[2][2]: orderNo2[2][3]: orderNo2[2][4]: orderNo2[2][5]: orderNo2[2][6]: orderNo2[2][7]: orderNo2[2][8]: orderNo2[2][9]: orderNo2[2][10]: orderNo2[2][11]: orderNo2[2][12]: orderNo2[2][13]: orderNo2[2][14]:
After $i = 1, the later ones are all empty. I don't know why the inner for loop does not work after the 1st outer for loop iteration