ale1981 Posted August 3, 2007 Share Posted August 3, 2007 Help! I have been pulling my hair out trying to get this to do what I want, but it doesnt seem to want to play. What I am trying to do is create an array with something like; [0]=>[itemNo]=>12345678 [itemCost]=>0.100, [1]=>[itemNo]=>22345678 [itemCost]=>0.150 and so on... But the array i get is; Array ( [0] => Array ( [itemNo] => 01400010 [itemCost] => ) [1] => Array ( [itemNo] => 01400020 [itemCost] => 0.01356 ) [2] => Array ( [itemNo] => 01400030 [itemCost] => 0.029 ) [3] => Array ( [itemNo] => 01400040 [itemCost] => 0.038 ) [4] => Array ( [itemNo] => 01400050 [itemCost] => 0.048 ) [5] => Array ( [itemNo] => 01400060 [itemCost] => 0.04502 ) [6] => Array ( [itemNo] => 01400070 [itemCost] => 0.054 ) [7] => Array ( [itemNo] => 01400080 [itemCost] => 0.062 ) [8] => Array ( [itemNo] => 01400090 [itemCost] => 0.075 ) ) Its creating an array inside an array? Here is my code; $item_list = array(); $i = 0; while ($item_row = mssql_fetch_assoc($item_res)) { $sql = 'SELECT rtrim(VJL.dbo.POP10110.ITEMNMBR), VJL.dbo.POP10110.UNITCOST FROM VJL.dbo.POP10110 INNER JOIN VJL.dbo.POP10100 ON VJL.dbo.POP10110.PONUMBER = VJL.dbo.POP10100.PONUMBER WHERE (VJL.dbo.POP10100.DOCDATE BETWEEN CONVERT(DATETIME, "01-08-2006", 103) AND CONVERT(DATETIME, "03-08-2007", 103)) AND (VJL.dbo.POP10110.ITEMNMBR = "' .$item_row['itemNumber'].'")'; $item_cost_res = mssql_query($sql) or die(); while ($item_cost_row = mssql_fetch_row($item_cost_res)) { //extract($item_cost_row); foreach ($item_cost_row as $key => $value) { strlen($value) == 8 ? $itemNo = $value : ''; strlen($value) < 8 ? $itemCost = $value : ''; if ($itemNo == $oldItemNo) { // compare costs } else { //echo $itemNo. '<br />'; $oldItemNo = $itemNo; $item_list[$i]['itemNo'] = $itemNo; $item_list[$i]['itemCost'] = $itemCost; $i++; } } } } print_r($item_list); Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/ Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 try <?php } else { $oldItemNo = $itemNo; $item_list[] = array( 'itemNo' = $itemNo, 'itemCost' => $itemCost); } Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-314927 Share on other sites More sharing options...
ale1981 Posted August 3, 2007 Author Share Posted August 3, 2007 ARG! it cant be that easy, SURELY!! Thanks Barand will give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-314932 Share on other sites More sharing options...
ale1981 Posted August 3, 2007 Author Share Posted August 3, 2007 That doesnt quite work either What I am trying to do is loop through each item number and work out the difference in cost during that period. So i was going to loop through each item and work out the difference and then put that difference into an array with the corresponding item number, does anybody have any ideas? i thought i was kind of on the right track?! Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-314943 Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 try <?php $item_cost_res = mssql_query($sql) or die(); while (list($itemNo, $itemCost) = mssql_fetch_row($item_cost_res)) { $item_list[$itemNo][] = $itemCost; } foreach ($item_list as $item => $costs) { if ($change = max($costs) - min($costs) ) echo "Period change for item $item : $change<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-315033 Share on other sites More sharing options...
ale1981 Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks Barand will give that a go Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-317343 Share on other sites More sharing options...
ale1981 Posted August 7, 2007 Author Share Posted August 7, 2007 That kind of works! Would it be possible to populate the array with; ([itemNo] => ([MinCost]=> [MaxCost]=> [Difference]=>) ) Thanks again for your help Barand. Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-317386 Share on other sites More sharing options...
ale1981 Posted August 7, 2007 Author Share Posted August 7, 2007 I think I got it; $item_cost_res = mssql_query($sql) or die(); while (list($itemNo, $itemCost) = mssql_fetch_row($item_cost_res)) { $item_list_temp[$itemNo][] = $itemCost; } foreach ($item_list_temp as $item => $costs) { if ($change = max($costs) - min($costs) ) $item_list[$item]['MinCost'] = min($costs); $item_list[$item]['MaxCost'] = max($costs); $item_list[$item]['Difference'] = $change; } } Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-317391 Share on other sites More sharing options...
ale1981 Posted August 7, 2007 Author Share Posted August 7, 2007 One last question and it will be perfect! How could I add the Item Description to the array, so it would be; ([itemNo]=> ([itemDesc]=> [MinCost]=> etc..... ) ) ? Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-317472 Share on other sites More sharing options...
ale1981 Posted August 7, 2007 Author Share Posted August 7, 2007 ...got it; $item_cost_res = mssql_query($sql) or die(); while (list($itemNo, $itemCost, $itemDesc) = mssql_fetch_row($item_cost_res)) { $item_list_temp[$itemNo][] = $itemCost; $item_list[$itemNo]['ItemDesc'] = $itemDesc; } foreach ($item_list_temp as $item => $costs) { if ($change = max($costs) - min($costs) ) $item_list[$item]['MinCost'] = min($costs); $item_list[$item]['MaxCost'] = max($costs); $item_list[$item]['Difference'] = $change; } } Quote Link to comment https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/#findComment-317541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.