Php 以优雅的方式根据关键结果抓取中间的x结果
我有一套产品。根据页面的状态,我将显示一个产品,然后最多显示4个其他产品。产品的结果集可以是大于5个产品的任意大小。我想经常展示5种产品。如果可用,我想展示下面的两种产品,即结果集和上面的两种产品 示例: 如果有10个结果,关键产品是5个。我想展示3,4,5,6,7 如果有10个结果,关键产品是9。我想展示6,7,8,9,10 如果有10个结果,关键产品是1。我想展示1,2,3,4,5 现在我正在使用min和max以及一些IFs来解决这个问题,它需要大量的代码行,当有一个优雅的解决方案时,我就是找不到它! 下面是数组结果集的示例Php 以优雅的方式根据关键结果抓取中间的x结果,php,structured-array,Php,Structured Array,我有一套产品。根据页面的状态,我将显示一个产品,然后最多显示4个其他产品。产品的结果集可以是大于5个产品的任意大小。我想经常展示5种产品。如果可用,我想展示下面的两种产品,即结果集和上面的两种产品 示例: 如果有10个结果,关键产品是5个。我想展示3,4,5,6,7 如果有10个结果,关键产品是9。我想展示6,7,8,9,10 如果有10个结果,关键产品是1。我想展示1,2,3,4,5 现在我正在使用min和max以及一些IFs来解决这个问题,它需要大量的代码行,当有一个优雅的解决方案时,我就是
$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++;
}
我还没有机会测试,因为我在手机上,但我能想到的解决问题的最简单方法是,让你有机会在未来改变这些固定的限制,不优雅,但看不到你现在有什么可以比较 如果需要,可以删除变量config,将其压缩一点,使其成为一个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);
您的代码可读性和性能是否足够?那么就没有问题了,也不太适合这样的问题。另外,一般来说,您应该显示您现在拥有的实际代码。我添加了我想要的代码。我希望能够将产品数从5重置为少于阵列中产品总数的任何数字。这非常完美。非常感谢。
// 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);