php - 以优雅的方式基于关键结果抓取中间的 x 结果
问题描述
我有一套产品。根据页面的状态,我显示了一种产品,然后显示了最多 4 种其他产品。产品的结果集可以是大于 5 个产品的任何大小。我想总是展示 5 个产品。如果可用,我想显示下面的 2 个产品(在结果集中)和上面的 2 个产品。
例子:
如果有 10 个结果并且关键产品是 5。我想显示 3、4、5、6、7。
如果有 10 个结果并且关键产品是 9。我想显示 6、7、8、9、10。
如果有 10 个结果并且关键产品是 1。我想显示 1、2、3、4、5。
现在我正在使用 min() 和 max() 以及一些“IF”来解决它,它需要大量的代码行,当有一个优雅的解决方案时,我只是找不到它!下面的示例数组结果集
$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++;
}
解决方案
如果您愿意,您可以删除变量配置,将其压缩一点,并使其成为 1-liner ;-) 我不擅长这些东西,所以可能有更高效和/或更短的选项。
// 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);
推荐阅读
- python - 使用 Python Web3.py 调用 Solidity 函数
- javascript - 试图将这个大的 if else 语句转换为循环
- javafx - 在 JavaFx 中使用 Slider 更新 GridPane
- c - 结构中的字符字符串数组,值不会根据主要(在C中)改变?
- c++11 - 在 unordered_map 中使用模板时 C++ 编译错误
- apache-kafka - Spring/Kafka - 配置属性优先级 - application.properties, bean(ProducerFactory)
- flutter - 特定国家/地区的城市和州选择器
- pyinstaller - Pyinstaller 与 vpython FileNotFoundError glow.min.js
- xamarin - 包含多个第三方绑定库的项目已禁用增量构建
- soap - 从 PNR 获取 Eticket 和航段