Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/63182-solved-foreach-and-arrays/
Share on other sites

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?!

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>";
}
?>

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;
	}
}

...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;
	}
}

 

:D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.