dadamssg87 Posted October 21, 2011 Share Posted October 21, 2011 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 :/ Quote Link to comment Share on other sites More sharing options...
kicken Posted October 21, 2011 Share Posted October 21, 2011 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 } } } Quote Link to comment Share on other sites More sharing options...
dadamssg87 Posted October 21, 2011 Author Share Posted October 21, 2011 this great, thanks! Quote Link to comment 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.