2013-11-20 34 views
2

重複陣列我只想做一兩件事,我有產品陣列,可以說,這樣的(簡單的方式):與array_slice

$products = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'); 

我outputing產品在滑塊(圖3產品),我有箭頭(左和右)。

主要我是第1頁($page = 0;),並使用array_slice($products, $page * 3, 3);,因此它會返回:

array('a', 'b', 'c'); 

當我點擊右箭頭,它會顯示:

array('d', 'e', 'f'); 

下一頁:

array('g', 'h', 'i'); 

下一個:

array('j', 'k'); 

這裏是要點和我的問題。我想在此頁返回array('j', 'k', 'a');

我該如何以最簡單的方式做到這一點?

PS:產品陣列是動態生成的。

PPS:我也想在其他方向做,即。當我點擊左箭頭,我在第一頁上,我想返回array('i', 'j', 'k');

任何想法,讚賞。

非常感謝。

回答

1
function getPortion($products, $page, $per_page) 
    { 
    $index = $page * $per_page; 

    // to get always positive module 
    $real_index = ($index % sizeof($products) + sizeof($products)) % sizeof($products); 

    $return = array_slice($products, $real_index, $per_page); 
    while (sizeof($return) < $per_page) 
     { 
     $return = array_merge($return, array_slice($products, 0, $per_page - sizeof($return))); 
     } 
    return $return; 
    } 

$products = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'); 

// 0 page 
echo json_encode(getPortion($products, 0, 3)); 
// ["a","b","c"] 

// 3 page 
echo json_encode(getPortion($products, 3, 3)); 
// ["j","k","a"] 

// -1 page 
echo json_encode(getPortion($products, -1, 3)); 
// ["i","j","k"] 

// 0 page, but per_page larger than array length. 
echo json_encode(getPortion(array('a', 'b', 'c'), 0, 5)); 
// ["a","b","c","a","b"] 

Demo向後做。

+0

卓越。最好的解決方案。非常感謝! – Legionar

2

您可以使用array_slice()從數組的兩端得到所需要的長度,然後用原來的數組進行合併,就像這樣:

$lengthToExtend = 3; 
$first = array_slice($products, 0, $lengthToExtend); 
$last = array_slice($products, -$lengthToExtend, $lengthToExtend); 
$products = array_merge($last, $products, $first); 

Demo.

+0

謝謝,現在當我再次考慮這個問題時,我可以看到解決方案,就像你寫的一樣。但是我想,在下一頁,'array'('j','k','a');'...後面我會考慮,它將如何完成,然後我需要獲得'array('b',' c','d');',等等...... – Legionar

+0

@Legionar:那麼只需增加'$ lengthToExtend'? [(見演示)](https://eval.in/70557) –

2

我瘋了解決方案:

<?php 
$products = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'); 

$perPage = 3; 
$offset = 9; 

if ($offset >= count($products)) $offset = 0; 

$page = array_slice($products, $offset, $perPage); 

if (count($products) <= ($offset+$perPage)) { 
    $page = array_merge($page, array_slice($products, 0, $perPage - count($page))); 
} 

print_r($page); 

Demo at Ideone.com

+0

你確定,這將工作?它看起來不像 - 當你$偏移= 0;它會導致一個問題... – Legionar

+0

偏移應該在0..count($ products)範圍內,那麼它工作正常 –

+0

但是它不會顯示只有2產品+第一個。 – Legionar

2

你所需要知道的就是從哪裏開始。用同樣的方法及其與endnext更換resetprev

<?php 

$a = array_map('chr', range(65,90)); 

$start = 22; 

# init 
$i = 0; while($i < $start) { next($a); $i++; } 

# for example, do this three times 
for($j = 0; $j < 3; $j++) { 
    $f = []; 
    for($e = 0; $e < 3; $e++) { 

     $c = current($a); 

     if($c === false) { 
     $c = reset($a); 
     } 

     $f[] = $c; 

     next($a); 

    } 
    print_r($f); 
} 

DEMO

DEMO(backwards)

0

這更簡單,我猜...完美的作品

$products = array_chunk($products, $limit, true); // true if you want keep keys 
$products_sliced = $products[$page-1]; 
+0

這當然不起作用......當在最後一頁上時,它不會返回'array('j','k','a');'...... – Legionar

相關問題