Jump to content

Logic help


dadamssg87

Recommended Posts

i have two multi-dimensional arrays that i'd like to manipulate to check a few things.

 

Their structures are different and i'm giving myself headaches trying to think through this.

 

I have a $sums array which has information on a product in a purchase order. The second array consists of items(products) that are going to update the purchase order. So the first array key of $sums correlates with the "order" of the $items array.

 

I'd like to write a function that:

 

1. Makes sure the first array key of $sums exists as an "order" value in the $items array.

2. Makes sure the $items price is greater than the $sums price

 

<?php
	$sums['23']['collected']['price'] = 30;
	$sums['73']['collected']['price'] = 60;
	$sums['89']['collected']['price'] = 90;

	$items['abc123']['order'] = '23';
	$items['abc123']['price'] = 14;
	$items['321cba']['order'] = '73';
	$items['321cba']['price'] = 79;

function check_items($sums,$items)
{
$error = "";

//Makes sure the first array key of $sums exists as "order" value in the $items array.
//If not -> 
$error = "Item #".$key." is not found in the items array.";

//where $key is either the first array key of $sums or the "order" value in $items because they are the same thing

//if found in $sums
if($items[$hash]['price'] < $sums[$items[$hash]['collected']['price'] )
{
    $error = "Item #".$items[$hash]['order']."'s price must not be lower than ".$sums[$items[$hash]['order']]['collected']['price'];
}

return $error;
}

// $sums['23'] would cause an error because its price of 14 is less than 30
// $sums['73'] would pass because it exists in $items and its price of 79 is greater than the $sums price of 60.
// $sums['89'] would cause an error because there is no $item corresponding to it
?>

 

I just don't know where to put the foreach loops or where to even start really :/

Link to comment
Share on other sites

Do one loop across the $sums array, storing the key and value in separate variables.  Then for each of those elements loop through the items array searching for an item with an order index that matches the current sum's key.

 

When you find one, compare the price indexes and show an error if necessary.  If you don't find one, show an error.

 

<?php
function check_items($sums,$items){

//$orderNum will be the key, ex '23'
//$sumInfo will be the array values
foreach ($sums as $orderNum=>$sumInfo){
	//Find an item with ['order']=$orderNum;
	$found=false;
	foreach ($items as $itemName=>$itemInfo){
		if ($itemInfo['order'] == $orderNum){
			$isGreater = $itemInfo['price'] > $sumInfo['collected']['price'];
			$found=true;
		}	
	}

	if (!$found || !$isGreater){
		//error
	}
}
}

Link to comment
Share on other sites

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.