PHP数据处理失败,错误不明确
用户请求一个产品类别,并说出他们想要的数量,即糖7磅 在数据库的搜索结果中,我有以下项目(每个项目都是不同的产品): 糖,8盎司.75 糖,1磅1.50 糖,2磅4.00 糖,5磅7.00 糖,10磅11.00 当前流程:PHP数据处理失败,错误不明确,php,data-processing,Php,Data Processing,用户请求一个产品类别,并说出他们想要的数量,即糖7磅 在数据库的搜索结果中,我有以下项目(每个项目都是不同的产品): 糖,8盎司.75 糖,1磅1.50 糖,2磅4.00 糖,5磅7.00 糖,10磅11.00 当前流程: 获取搜索结果 检查任何一个结果的最便宜选项(只有10磅可以满足要求)或其中一个结果的倍数(但需要1个中的许多匹配标准,因此7 X 1磅) 将各个产品ID放入一个数组中 获得1:1的排列,但在此范围内,使用一些代码添加最多3个重复(因为5磅+2 X 1磅是最便宜的选择,而不是
- 获取搜索结果
- 检查任何一个结果的最便宜选项(只有10磅可以满足要求)或其中一个结果的倍数(但需要1个中的许多匹配标准,因此7 X 1磅)
- 将各个产品ID放入一个数组中
- 获得1:1的排列,但在此范围内,使用一些代码添加最多3个重复(因为5磅+2 X 1磅是最便宜的选择,而不是5磅+2磅)
- 在这种情况下,检查不同的数量单位(盎司对磅),并能够转换和比较
- 比较最便宜的和返回最便宜的
- 我甚至不考虑排列中有6个以上的元素,以剔除不太可能的选项并减少开销
function processCombos($arr, $qty_search, $qty_unit_search){ //$arr is the dataset, $qty_search is 7, $qty_unit_search is 1 for lbs
$combo=array();
$pid_arr = arrayifyProductIDs($arr);
$count = count($pid_arr);
$members = pow(2,$count);
for ($i = 0; $i < $members; $i++) {
$b = sprintf("%0".$count."b",$i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1'){
$out[] = $pid_arr[$j];
}
}
$minLength=2;
$out_max = count($out);
if ($out_max >= $minLength) {
// now add in different repeats of each of them
$repeat_max = 3;
$indiv = array();
for($k=0;$k<$out_max;$k++){
$tmp = array();
for ($r = 0; $r < $repeat_max; $r++) $tmp[$r] = array_fill(0, $r + 1, $out[$k]);
$indiv[] = $tmp;
}
$x_ct = count($indiv[0]);
$y_ct = count($indiv[1]);
$z_ct = count($indiv[2]) > 0 ? count($indiv[2]): 0;
$perm = array();
for($x=0;$x<$x_ct;$x++){
for($y=0;$y<$y_ct;$y++){
if($z_ct > 0){
for($z=0;$z<$z_ct;$z++){
$perm = array_merge($indiv[0][$x],$indiv[1][$y],$indiv[2][$z]);
}
}else{
$perm = array_merge($indiv[0][$x],$indiv[1][$y]);
}
$p=0;
$max_p=count($perm);
if($max_p >=7){
}else{
$product_ids = array();
$qty = 0;
$price = 0;
while($p < $max_p){
$product_id = $perm[$p];
$data = $arr[$product_id];
if(!$data['qty_unit_id'] OR !$data['qty']){continue;} // go to the next one if it doens't have qty or qty_unit
if($data['qty_unit_id'] == $qty_unit_search){
$product_ids[] = $product_id;
$qty += $data['qty'];
$price += $data['price'];
}else{
$unit_to_convert_data = getQtyUnitName($qty_unit_search);
$unit_to_convert = $unit_to_convert_data['abbr'];
$unit_to_convert_type = $unit_to_convert_data['conv_file'];
if($unit_to_convert_type == $data['conv_file']){
if($data['conv_file'] == "Mass"){
$product_conv = new PhpUnitsOfMeasure\PhysicalQuantity\Mass($data['qty'], $data['qty_unit']);
}else{
$product_conv = new PhpUnitsOfMeasure\PhysicalQuantity\Volume($data['qty'], $data['qty_unit']);
}
$data['qty_CONV'] = number_format($product_conv->toUnit($unit_to_convert),3,".",",");
$product_ids[] = $product_id;
$qty += $data['qty_CONV'];
$price += $data['price'];
}
}
$p++;
}
if(count($combo)==0 AND $qty >= $qty_search){
$combo = array('product_ids' => $product_ids, 'qty' => $qty, 'price' => $price);
}elseif(($qty >= $qty_search AND $price < $combo['price']) OR
($qty >= $qty_search AND $price == $combo['price'] AND $qty > $combo['qty'])){
$combo = array('product_ids' => $product_ids, 'qty' => $qty, 'price' => $price);
}else{
}
}/// i think it should go here
}
}
}
}
return $combo;
}
函数processCombos($arr、$qty\u search、$qty\u unit\u search){/$arr是数据集,$qty\u search是7,$qty\u unit\u search是1表示lbs
$combo=array();
$pid_arr=arrayifyProductId($arr);
$count=计数($pid\U arr);
$members=pow(2$count);
对于($i=0;$i<$members;$i++){
$b=sprintf(“%0”。$count.b”,$i);
$out=array();
对于($j=0;$j<$count;$j++){
如果($b{$j}=='1'){
$out[]=$pid_arr[$j];
}
}
$minLength=2;
$out_max=计数($out);
如果($out\u max>=$minLength){
//现在,添加每种方法的不同重复
$repeat_max=3;
$indiv=array();
对于($k=0;$k 0?计数($indiv[2]):0;
$perm=array();
对于($x=0;$x脚本头过早结束
通常意味着,由于httpd.conf中RLimitCPU
和RLimitMEM
指令的资源限制,在将头发送到web服务器之前,您的脚本会被终止。请更改这些指令,以便为应用程序提供更多的CPU和内存,或者编写更高效的代码(3 for for循环是指处理记录,是它里面的块的3倍。考虑重写它) Hm,在这个列表中,我们可以从理论上讲,因为.LB的5磅。75可以组合到那些相同或更低的价格的2。你想这样做,还是有优势(额外维度)?哪个应该更喜欢大包装?我们是否应该找到相同的“最佳”解决方案,然后选择项目数最少的解决方案?@Wrikken大包装是最佳的,所有其他因素都是相同的。这将在所包含代码的最终if/elseif中处理。也许为了节省时间/资源,我可以开始排除不合适的数量太大了,比如10磅的产品?!?我有一个更干净的解决方案,它在一个数组中加载所有排列,然后在一个循环中处理(比较数量和价格数据),但它超过了我共享主机上的内存(我相信是124MB)。有什么更有效的解决方案吗?谢谢!