2

I just want to do one thing, I have an array with products, lets say like this (in simple way):

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

I am outputing that products in a slider (showing 3 products), and I have arrows (left and right).

Primary I am on page 1 ($page = 0;), and using array_slice($products, $page * 3, 3);, so it will return:

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

When I click arrow right, it will show:

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

Next:

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

Next:

array('j', 'k');

And here is the point and my question. I want on this page to return array('j', 'k', 'a');

How can I do it in the simpliest way?

PS: array with products is generated dynamically.

PPS: I also want to do it in other direction, ie. when I click left arrow, and I am on the first page, I want to return array('i', 'j', 'k');

Any ideas are appreciated.

Thanks a lot.

0

5 Answers 5

2

You can use array_slice() to get the required length from both ends of the array and then merge it with the original array, like so:

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

Demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, now when I am thinking about that again, I can see the solution, like you wrote. But I am thinking, how it will be done, on next page, after array('j', 'k', 'a');... Then I will need to get array('b', 'c', 'd');, and so on...
@Legionar: Then just increase $lengthToExtend? (See demo)
2

My crazy solution:

<?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

2 Comments

Are you sure, this will work? It doesnt look like - when you $offset = 0; it will cause a problem...
Offset should be in 0..count($products) range, then it works fine
2

All you need to know here is where to begin. The same way its done backwards by replacing reset with end and next with prev

<?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)

Comments

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.

1 Comment

Excelent. The nicest solution. Thanks a lot!
0

This is more simple, I guess... works perfect for me

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

1 Comment

This will of course not work... When on the last page, it will not return array('j', 'k', 'a');...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.