pattygeek
pattygeek

Reputation: 103

Randomly group of array content in PHP

I have this kind of array

 Array(
        [0]=>Array(
                 [title]       => 'Title 0'
                 [link]        => 'Link 0'
                 [excerpt]     => 'Excerpt 0'
                 [content]     => 'Content 0'
             )
        [1]=>Array(
                 [title]       => 'Title 1'
                 [link]        => 'Link 1'
                 [excerpt]     => 'Excerpt 1'
                 [content]     => 'Content 1'
             )
        [2]=>Array(
                 [title]       => 'Title 2'
                 [link]        => 'Link 2'
                 [excerpt]     => 'Excerpt 2'
                 [content]     => 'Content 2'
             )
    )

How can I shuffle between [content],[excerpt] and [title],[link] So basically it's gonna look something similar like this

 Array(
        [0]=>Array(
                 [title]       => 'Title 0'
                 [link]        => 'Link 0'
                 [excerpt]     => 'Excerpt 1'
                 [content]     => 'Content 1'
             )
        [1]=>Array(
                 [title]       => 'Title 1'
                 [link]        => 'Link 1'
                 [excerpt]     => 'Excerpt 2'
                 [content]     => 'Content 2'
             )
        [2]=>Array(
                 [title]       => 'Title 2'
                 [link]        => 'Link 2'
                 [excerpt]     => 'Excerpt 0'
                 [content]     => 'Content 0'
             )
    )

If shuffle entire array content I can do that, but this case I still confuse, anyone can help me?

Thank you

Patty

Upvotes: 1

Views: 81

Answers (1)

Syscall
Syscall

Reputation: 19780

You could use array_column() to get the values, then, shuffle the keys, and finally, update the contents using the same "shuffled" key:

$array = array(
    array(
        'title'   => 'Title 0',
        'link'    => 'Link 0',
        'excerpt' => 'Excerpt 0',
        'content' => 'Content 0'
    ),
    array(
        'title'   => 'Title 1',
        'link'    => 'Link 1',
        'excerpt' => 'Excerpt 1',
        'content' => 'Content 1'
    ),
    array(
        'title'   => 'Title 2',
        'link'    => 'Link 2',
        'excerpt' => 'Excerpt 2',
        'content' => 'Content 2'
    )
);

// get data
$excerpts = array_column($array, 'excerpt');
$contents = array_column($array, 'content');
// get keys and shuffle them
$keys = array_keys($array);
shuffle($keys);
// iterate over array (and pass $values as reference)
foreach ($array as $key => &$values) {
    // get the new shuffled key
    $rand_key = $keys[$key];
    // update content
    $values['excerpt'] = $excerpts[$rand_key];
    $values['content'] = $contents[$rand_key];
}
print_r($array);

Could outputs:

Array
(
    [0] => Array
        (
            [title] => Title 0
            [link] => Link 0
            [excerpt] => Excerpt 0
            [content] => Content 0
        )

    [1] => Array
        (
            [title] => Title 1
            [link] => Link 1
            [excerpt] => Excerpt 2
            [content] => Content 2
        )

    [2] => Array
        (
            [title] => Title 2
            [link] => Link 2
            [excerpt] => Excerpt 1
            [content] => Content 1
        )

)

Upvotes: 2

Related Questions