dpq39825 2018-06-08 21:47
浏览 25
已采纳

以优雅的方式基于关键结果获取中间x结果

I have a set of products. Based on the state of the page, I'm displaying one product and then up to 4 other products. The result set of the products can be any size greater than 5 products. I want to ALWAYS show 5 products. If available I want to show the 2 products below(in the result set) and the 2 products above.

Examples:

If there are 10 results and the key product is 5. I want to show 3,4,5,6,7.

If there are 10 results and the key product is 9. I want to show 6,7,8,9,10.

If there are 10 results and the key product is 1. I want to show 1,2,3,4,5.

Right now I'm using min() and max() and some "IF"s to figure it out and it takes a ridiculous number of lines of code, when there is an elegant solution out there, I'm just not finding it! example array result set below

$similar_products = array(
  array(
    "id" => 1,
    "title" => "Product One"
  ),
  array(
    "id" => 2,
    "title" => "Product Two"
  ),
  array(
    "id" => 3,
    "title" => "Product Three"
  ),
  array(
    "id" => 4,
    "title" => "Product Four"
  ),
  array(
    "id" => 5,
    "title" => "Product Five"
  ),
  array(
    "id" => 6,
    "title" => "Product Six"
  ),
  array(
    "id" => 7,
    "title" => "Product Seven"
  ),
  array(
    "id" => 8,
    "title" => "Product Eight"
  ),
  array(
    "id" => 9,
    "title" => "Product Nine"
  ),
  array(
    "id" => 10,
    "title" => "Product Ten"
  )
);
$i = 8; //change this value to test different key product array positions
$arrOut = array();
$floor = 0;
if($i <= 1) { //the key product is either in the first or second position in the array
  $floor = 0;
  $arrOut[] = $similar_products[0];
  $arrOut[] = $similar_products[1];
  $arrOut[] = $similar_products[2];
  $arrOut[] = $similar_products[3];
  $arrOut[] = $similar_products[4];
} elseif((count($similar_products)-1)-$i <= 1) {  //the key product is either in the last or second to last in the array
  $floor = count($similar_products)-5;
  $arrOut[] = $similar_products[count($similar_products)-5];
  $arrOut[] = $similar_products[count($similar_products)-4];
  $arrOut[] = $similar_products[count($similar_products)-3];
  $arrOut[] = $similar_products[count($similar_products)-2];
  $arrOut[] = $similar_products[count($similar_products)-1];
} else {  //otherwise, just grab two above and two below
  $floor = $i-2;
  $arrOut[] = $similar_products[$i-2];
  $arrOut[] = $similar_products[$i-1];
  $arrOut[] = $similar_products[$i];
  $arrOut[] = $similar_products[$i+1];
  $arrOut[] = $similar_products[$i+2];
}
$x = $floor;  //set x, our counter, to the floor (floor = the very first output postion)
foreach($arrOut as $ao) {
  if($x == $i) {  //current key product
    echo "<strong>" . $ao['id'] . ":" . $ao['title'] . "</strong><hr/>";
  } else {  //other NON key products
    echo $ao['id'] . ":" . $ao['title'] . "<hr/>";
  }
  $x++;
}
  • 写回答

2条回答 默认 最新

  • douan1893 2018-06-11 19:02
    关注

    If you want you can remove the variable config, condense it a bit, and make it a 1-liner ;-) I'm not good with this stuff, so there are probably more efficient and/or shorter options.

    // Set array index, starts with 0
    // If you need to find with specific ID, just find the index by the ID
    $primaryIndex = 4;// change this number to test
    
    // How many extra items to show
    // Must be divisible by 2
    $extraToShow = 4;
    
    // Find total items available - 1 to work with array indexes
    $maxIndex = count($similar_products) - 1;
    
    // Find the slice start
    $low = min($maxIndex - $extraToShow, max(0, $primaryIndex - 1 - $extraToShow / 2));
    
    // Slice to needed
    $items = array_slice($similar_products, $low, $extraToShow + 1);
    
    var_dump($items);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画