PHP array loop, accessing by numeric index
I have two arrays at the bottom, I'm looking to output their values by a numeric index. Seems so simple yet I cannot get it to work for the life of me. I'm nesting these into a few other foreach loops.
$x = $ppro_model->orderedByDate($val->ProdCode);
$xx = $ppro_model->receivedByDate($val->ProdCode);
$ordQty = arrayDateBuilder($x, 'ordQUANTITY', $daterange);
$recQty = arrayDateBuilder($xx, 'recQUANTITY', $daterange);
$ordQty and $recQty will ALWAYS have the same amount of elements. So ideally I would like to be able to do the following:
for($i = 0; $i <= count($ordQty); $i++){
echo "<td>";
echo $ordQty['recQUANTITY'][$i];
echo $recQty['ordQUANTITY'][$i];
echo "<td>";
}
but I'm definitely doing something wrong. Any advice would be appreciated.
I also tried:
echo $ordQty[$i];
to not avail.
array (size=7)
0 =>
array (size=2)
'DATE' => string '20141110' (length=8)
'ordQUANTITY' => string '55.00' (length=5)
1 =>
array (size=2)
'DATE' => string '20141111' (length=8)
'ordQUANTITY' => string '80.00' (length=5)
2 =>
array (size=2)
'DATE' => string '20141112' (length=8)
'ordQUANTITY' => string '70.00' (length=5)
3 =>
array (size=2)
'DATE' => string '20141113' (length=8)
'ordQUANTITY' => string '135.00' (length=6)
4 =>
array (size=2)
'DATE' => string '20141114' (length=8)
'ordQUANTITY' => string '70.00' (length=5)
5 =>
array (size=2)
'DATE' => string '20141115' (length=8)
'ordQUANTITY' => string '120.00' (length=6)
6 =>
array (size=2)
'DATE' => string '20141116' (length=8)
'ordQUANTITY' => string '0' (length=1)
then one like:
array (size=7)
0 =>
array (size=2)
'DATE' => string '20141110' (length=8)
'recQUANTITY' => string '0' (length=1)
1 =>
array (size=2)
'DATE' => string '20141111' (length=8)
'recQUANTITY' => string '0' (length=1)
2 =>
array (size=2)
'DATE' => string '20141112' (length=8)
'recQUANTITY' => string '0' (length=1)
3 =>
array (size=2)
'DATE' => string '20141113' (length=8)
'recQUANTITY' => string '0' (length=1)
4 =>
array (size=2)
'DATE' => string '20141114' (length=8)
'recQUANTITY' => string '0' (length=1)
5 =>
array (size=2)
'DATE' => string '20141115' (length=8)
'recQUANTITY' => string '0' (length=1)
6 =>
array (size=2)
'DATE' => string '20141116' (length=8)
'recQUANTITY' => string '0' (length=1)
Answer
Solution:
You had your array expressions backward, it should be
$ordQty[$i]['ordQUANTITY']
. Also, your loop condition was wrong, it should be$i < count($ordQty)
, not<=
, since array indexes go from0
tocount-1
.It can be simplified a bit using
foreach
.