简体   繁体   中英

php get random unique value from an array without repeating & also preserve & get the key

I am new to php, I am trying to get random value from an array of lets say 4 values without repeating the value for the array group & also get the key for that values . I have multiple places on page where i echo the value . i tried array_rand , shuffle() but they repeat the values not keeping uniques.

$fruit = [
'f1' => 'mango',
'f2' => 'pear',
'f3' => 'apple',
'f4' => 'banana',
];
$key = array_keys($fruit);
$fruitvalue = $key[shuffle($key)];

echo $key & $fruitvalue somewhere on page

Shuffle() , array_rand() isnt helping me keep the values unique in a single cycle of 4 if i want the random values to appear 120 times on page also i cant use i=1;i<120;i++ as that "120" is not fixed ,on some places its 150 while different at other pages .. IF i want to show random value at 120 places , these values should be unique in each group of 4 but a same time random and so on in each successive group so 1st group [ie 1st 4 values] in list of 120 can have random but be unique like pear,banana,apple,mango then next 4 can be again random but unique ie. no repeat & so on till end...I also echo they key infront of the value so that too needs to be preserved . How do i achieve it ?

I'd do something like this

$keys = shuffle(array_keys($fruit));

Then you could use array_shift to take the first element of the array while shortening the array.

$fruit1 = $fruit[array_shift($keys)];
$fruit2 = $fruit[array_shift($keys)];
$fruit3 = $fruit[array_shift($keys)];
$fruit4 = $fruit[array_shift($keys)];

array_shift

You can also do just:

$k = array_rand($fruit);
$v = $fruit[$k];

This is the way to do it when you have an associative array.

One way to achieve your desired result is to write a function which returns a random value from the $fruit array, going through all values in the array and then shuffling the array before returning the next set of values. Since we can't shuffle an array without losing the keys, this function stores the keys of the $fruit array and shuffles them instead:

function random_fruit() {
    static $i = 0;
    static $fruit = [
        'f1' => 'mango',
        'f2' => 'pear',
        'f3' => 'apple',
        'f4' => 'banana',
    ];
    // would be nice to write:
    //   static $keys = array_keys($fruit); 
    // but PHP doesn't allow it so we have to assign the value
    // the first time we call the function
    static $keys;
    if ($i == 0) $keys = array_keys($fruit);
    $length = count($fruit);
    if ($i % $length == 0) shuffle($keys);
    return array($keys[$i % $length], $fruit[$keys[$i++ % $length]]);
}

Then every time you want a new fruit, you can simply call the random_fruit function. For example, to output 20 random fruits:

for ($j = 0; $j < 20; $j++) {
    list($key, $fruitvalue) = random_fruit();
    echo "$key: $fruitvalue\n";
    if ($j % 4 == 3) echo "-----\n";
}

Sample output:

f4: banana
f2: pear
f1: mango
f3: apple
-----
f4: banana
f1: mango
f3: apple
f2: pear
-----
f3: apple
f2: pear
f4: banana
f1: mango
-----
f3: apple
f1: mango
f4: banana
f2: pear
-----
f4: banana
f1: mango
f2: pear
f3: apple
-----

Demo on 3v4l.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM