I'm designing a package engine for my catalog. Here you can add a certain ammount of products to the package and a discount. When you order products the script have to detect which package deals apply to your order.
Here is my code:
// packages
$packages["package1"] = array(1,1,2);
$packages["package2"] = array(1,2);
//orderlist
$orderlist = array(1,1,2,1,2,2);
// put the order list in a temp array
$temp_product_array = $orderlist;
foreach($packages as $pname => $package_array)
{
$no_more_package = 0;
do
{
// put the package products in a temp array
$temp_package_array = $package_array;
$is_packages_array = array_intersect($temp_package_array,$temp_product_array);
// if all package values are present
if(count($is_packages_array) == count($temp_package_array))
{
// add package name
$packages_in_order[] = $pname;
// filter the package out of the product list but keep duplicate values
foreach($temp_product_array as $key1 => $pid1)
{
foreach($temp_package_array as $key2 => $pid2)
{
if($pid1==$pid2)
{
unset($temp_product_array[$key1]);
unset($temp_package_array[$key2]);
break; // after removing go to the next product to prevent double erasing
}
}
}
}
else
{
$no_more_package = 1;
}
}
while($no_more_package<1);
}
print_r($packages_in_order);
print_r($temp_product_array);
The result is:
Array ( [0] => package1 [1] => package1 ) Array ( [5] => 2 )
But I want the result to be:
Array ( [0] => package1 [1] => package2 ) Array ( [5] => 2 )
I tried array_diff
, array_intersect
but they all do not work well with duplicate values.
Does anyone has a better/working way of solving this?
(PS because of different sources I cannot work with associative arrays)
I would devide the problem. Part of it is to locate the package inside the list. An existing function that does exactly that has been named
consecutive_values
in a probably related question: Searching for consecutive values in an array.With it is it possible to locate an array within another array in the exact order. This is probably what you want.
The part left is to search for the packages then and is pretty straight forward. If you understood your question right, you want to return left-overs as well:
Output:
Edit: Searching for the same package multiple times needs a slight modification. Here the an inner while loop is created that needs a break if the current package is not found: