PHP 数据处理失败,错误不明确

标签 php data-processing

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

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

Sugar, 8 oz .75

Sugar, 1 lb 1.50

Sugar, 2 lb 4.00

Sugar, 5 lb 7.00

Sugar, 10 lb 11.00

当前进程:

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

这很好用,除非有 > 9 个产品 ID(有些产品 ID > 25),然后我在日志中收到此错误(浏览器中出现完全不相关的错误):脚本 header 过早结束

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

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

最佳答案

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

关于PHP 数据处理失败,错误不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053883/

相关文章:

php - 获取特定模型实例的事件日志(使用 spatie/laravel-activitylog)

r - 使用 R 提取另一种语言的 html 表

java - 处理大量元素时 hibernate 内存不足异常

java - 简单的基于 java 的工作流管理器/数据工作流,能够启动分机。应用程序,调用网络服务等

python - 将一行分布在共享相同键的其他行上

PHP正则表达式: match several inclusion/exclusion rules for matches consisting of a complex character set

php - 如何在Mailchimp上的列表成员中添加标签?

php - 我如何用 <a> 发送参数以在 smarty & Php 中设置查询 MySQL

php - jquery 仅读取 div 表格单元格中的更多链接

python - 在 python 中处理大量数据,我应该使用多线程/进程吗?