Shuffle an Array in PHP

Shuffle an array in PHP

As $bb is an array of arrays, shuffle() won't randomise the sub-array, try shuffle on the nested array as follows:

shuffle($bb['slides']);

shuffle an array with a limit in PHP

Here is a possible solution in one pass :

Try to swap each element at position i with an element between i (stay in place) and i+x. I look only forward to avoid swaping an element several times. And I need an extra array to flag the already swapped elements. I don't need to process them in the future as they were already moved.

function shuffle_array($a, $limit)
{
$result = $a ;
$shuffled_index = array() ; // list of already shuffled elements

$n = count($result);
for($i = 0 ; $i < $n ; ++$i)
{
if( in_array($i, $shuffled_index) ) continue ; // already shuffled, go to the next elements

$possibleIndex = array_diff( range($i, min($i + $limit, $n-1)), $shuffled_index) ; // get all the possible "jumps", minus the already- shuffled index
$selectedIndex = $possibleIndex[ array_rand($possibleIndex) ]; // randomly choose one of the possible index

// swap the two elements
$tmp = $result[$i] ;
$result[$i] = $result[$selectedIndex] ;
$result[$selectedIndex] = $tmp ;

// element at position $selectedIndex is already shuffled, it needs no more processing
$shuffled_index[] = $selectedIndex ;
}

return $result ;
}

$array = [92,12,2,18,17,88,56];
$limit = 2 ;

shuffle_array($array, $limit); // [2, 18, 92, 12, 17, 56, 88]

I expect more elements to stay in place than in the solution of Kerkouch, as some elements can have very few remaining free choices.

Shuffle an array in php does not really shuffle array

shuffle() alters the array in place instead of returning a new array with different order of elements. So all you need to do is:

shuffle($first);

Instead of:

$first = shuffle($first);

PHP - Extract first 3 elements of an array - Shuffle it - and add them back to the orig. array

Yes, its possible. You were on right track. With a few tweaks it worked well.

Code:

public function shuffling($data) {

// Return early there are no items to shuffle.
if (!is_array($data['slider'])) {
return $data;
}

$sliced_array = array_slice($data["slider"], 0, 3, TRUE);
// Shuffle the keys and loop through them to create a new, randomized array of images.
shuffle($sliced_array);

foreach ($sliced_array as $key => $value) {
$data['slider'][$key] = $value;
}
return $data;
}

I've tried with sample array like:

shuffling(["slider" => [
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
]]);

And result is:

Array
(
[slider] => Array
(
[0] => B
[1] => C
[2] => A
[3] => D
[4] => E
)

)

Note: shuffle is already function defined in php. That's why I've changed name to shuffling.

What is the best way to randomize an array's order in PHP without using the shuffle() function?

You could use the Fisher-Yates shuffle.

Shuffle array key and values php

$array = array('yes' => 'oui', 'no' => 'non', 'hello' => 'bonjour');

foreach($array as $key => $value) {
if (0 === rand(0,1)) {
$array[$value] = $key;
unset($array[$key]);
}
}

How to randomize an array of objects in PHP?

But the objects are in an array, not another object so you can use shuffle...

PHP Random Shuffle Array Maintaining Key = Value

The first user post under the shuffle documentation:

Shuffle associative and
non-associative array while preserving
key, value pairs. Also returns the
shuffled array instead of shuffling it
in place.

function shuffle_assoc($list) { 
if (!is_array($list)) return $list;

$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = $list[$key];
}
return $random;
}

Test case:

$arr = array();
$arr[] = array('id' => 5, 'foo' => 'hello');
$arr[] = array('id' => 7, 'foo' => 'byebye');
$arr[] = array('id' => 9, 'foo' => 'foo');
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));


Related Topics



Leave a reply



Submit