1

What I currently have is an array with image links for a slider. The goal is to get a random starting position in the array, but then follow the sequence of the array.

Basic array: 1 2 3 4 5 6 7 8 9 10 Random start: 4 5 6 7 8 9 10 1 2 3

$attachments = get_children(
    array(
       'post_parent' => $attachment_holder['ID'],
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'rand'));
    }

$attachments2 = get_children(
    array(
        'post_parent' => $attachment_holder['ID'],
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'rand'));


    shuffle($attachments2);
    $attachments2 = reset($attachments2); 
    $attachments2 = array($attachments2);

    $attachments = array_merge($attachments2, $attachments);

Above just outputs a random first image, and then starts with regular sequence.

So: 4 1 2 3 4 5 6 7 8 9 10

I have the feeling array_chunk or array_slice should help me out, but I'm not too sure.

1 Answer 1

2

This would do:

<?php
$input = range(1, 20);

$start = array_rand($input);
$input = array_merge(array_slice($input, $start), array_slice($input, 0, $start));

print_r($input);

Or inplace:

<?php
$input = range(1, 20);

array_splice($input, 0, 0, array_splice($input, array_rand($input)));

print_r($input);
Sign up to request clarification or add additional context in comments.

Comments

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.