You need to change the values in this codes
define('TO_SHOW',7); // number of items to show in your case it is 7
$selected = 1; //// which one you need need at center.
和你的數組;
執行此,並嘗試:
<?php
define('TO_SHOW',7); // number of items to show in your case it is 7
function change_order($arry, $sel){
$arr_cnt = count($arry);
$shift_count = $arr_cnt - (ceil(TO_SHOW/2)-$sel);
for($i=0; $i<$shift_count; $i++){
array_push($arry, array_shift($arry));
}
return array_slice($arry, 0, TO_SHOW);
}
$arr = array(array(
"id" => 1,
"date" => 2012-09-29
),
array(
"id" => 2,
"date" => 2012-09-29
),
array(
"id" => 3,
"date" => 2012-09-29
),
array(
"id" => 4,
"date" => 2012-09-29
),
array(
"id" => 5,
"date" => 2012-09-29
),
array(
"id" => 6,
"date" => 2012-09-29
),
array(
"id" => 7,
"date" => 2012-09-29
),
array(
"id" => 8,
"date" => 2012-09-29
),
array(
"id" => 9,
"date" => 2012-09-29
),
array(
"id" => 10,
"date" => 2012-09-29
),
array(
"id" => 11,
"date" => 2012-09-29
),
array(
"id" => 12,
"date" => 2012-09-29
),
array(
"id" => 13,
"date" => 2012-09-29
),
array(
"id" => 14,
"date" => 2012-09-29
)
);
$selected = 1; //// centre one
$test = change_order($arr, $selected);
echo "<pre>";
print_r($test);
?>
什麼是你目前的解決方案?你在使用'ksort()'或'usort()'時遇到了什麼問題? –
我已經計算出原始數組將被轉移到左側還是右側,以及將從轉變中「切斷」哪些鍵。然後我創建一個新的數組並開始從[3]中填充它,然後將截斷鍵添加到左側或右側。這是非常手動的,讓我感覺,我應該以其他方式做... – acrmuui