【问题标题】:PHP Data Processing Failing With Ambiguous ErrorPHP 数据处理因模棱两可的错误而失败
【发布时间】:2014-02-26 21:36:23
【问题描述】:

用户请求产品类别并说出他们想要的数量,即糖 7 磅。

在搜索结果中,从数据库中,我有以下项目(分别是不同的产品):

糖,8 盎司 0.75

糖,1 磅 1.50

糖,2 磅 4.00

糖,5 磅 7.00

糖,10 磅 11.00

当前进程:

  • 获取搜索结果
  • 检查任何一项结果(仅 10 磅即可满足)或其中一项结果的倍数(但需要 1 中的许多项才能符合条件,因此 7 X 1 磅),寻找最便宜的选项
  • 将各个产品 ID 放入一个数组中
  • 获得 1:1 排列,但在其中,使用一些代码添加最多 3 次重复(因为 5 lbs + 2 X 1 lbs 是最便宜的选择,而不是 5 lbs + 2 lbs)
  • 在此,检查不同的数量单位(盎司与磅),并能够进行转换和比较
  • 比较最便宜的并返回最便宜的
  • 我什至忽略了排列中超过 6 个元素的位置,以清除不太可能的选项并减少开销

这工作正常,除非有 > 9 个产品 ID(对于某些我有 > 25 个),然后我在日志中收到此错误(并且在浏览器中出现完全不相关的错误):脚本标头过早结束

这是很多代码。对不起,只想彻底!有没有更有效/更有效的方法?

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

【问题讨论】:

  • 嗯,在该列表中,理论上我们可以discard the 1lb & 2lb as being in our solution directly,因为 0.5 磅的 0.75 可以以相同或更低的价格组合到这 2 个。你想这样做,还是有一个优势(额外维度)应该更喜欢更大的包装?我们是否应该找到同等的“最佳”解决方案,然后选择项目数量最少的解决方案?
  • @Wrikken 较大的包装是最佳选择,所有其他因素都相同。这是在包含代码的最终 if/elseif 中处理的。也许为了节省时间/资源,我可以开始排除太大的数量,即 10 磅的产品?!?

标签: php data-processing


【解决方案1】:

Premature end of script headers 通常意味着由于 httpd.conf 中 RLimitCPURLimitMEM 指令中的资源限制,您的脚本在将标头发送到 Web 服务器之前被终止。要么更改这些指令以允许应用程序使用更多 CPU 和内存,要么编写更高效的代码(3 个 for 循环意味着处理 record^3 倍于其中的块。考虑重写它。)

【讨论】:

  • 我有一个更简洁的解决方案,它将所有排列加载到一个数组中,然后在一个循环中处理(比较数量和价格数据),但它超过了我共享主机上的内存(124MB,我相信)。对更有效的解决方案有任何想法吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 2023-03-08
  • 2021-05-07
  • 1970-01-01
相关资源
最近更新 更多