2

I need to random a single element from the array. I have code ;

     if (isset($_POST['losuj'])) {
   $arr = [
   'chleb' => 'skiny/1.jpg',
   'mienso' => 'skiny/2.jpg',
   'mienso2' => 'skiny/2.jpg',
   'mienso3' => 'skiny/2.jpg',
   'mienso4' => 'skiny/2.jpg',
   'mienso5' => 'skiny/2.jpg',
   'Hasasdasd' => 'skiny/2.jpg',
   ];

     foreach($arr as $key => $value) {
        $keys = array_rand( $arr, 1);
        echo $keys;
     }
 }

And its didnt working. Any tips ?

Season6
  • 39
  • 4
  • `$keys` variable displaying what? Refer this http://php.net/manual/en/function.array-rand.php – KMS Jan 29 '18 at 12:18
  • What do you mean by didn't work? If you are looking for a single random element remove the foreach and simply use the call to array_rand once. – Dave Jan 29 '18 at 12:26
  • Possible duplicate of [Selecting a random element from a PHP associative array](https://stackoverflow.com/questions/7209127/selecting-a-random-element-from-a-php-associative-array) – splash58 Jan 29 '18 at 14:08

7 Answers7

1

You can use array_keys to get the keys in a indexed array.
The just use array_rand just like you did to pick one and echo the $arr associative key.

$keys = array_keys($arr);
$random = $keys[array_rand($keys,1)];
Echo $random . " => " . $arr[$random];

https://3v4l.org/miacb

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Just use array_rand($arr,1) without foreach loop

Kuba Zabielski
  • 146
  • 2
  • 10
0

With PHP we can use the function array_rand()

More information can be found at:

http://php.net/manual/en/function.array-rand.php https://www.w3schools.com/php/func_array_rand.asp

$arr = [
  'chleb' => 'skiny/1.jpg',
  'mienso' => 'skiny/2.jpg',
  'mienso2' => 'skiny/2.jpg',
  'mienso3' => 'skiny/2.jpg',
  'mienso4' => 'skiny/2.jpg',
  'mienso5' => 'skiny/2.jpg',
  'Hasasdasd' => 'skiny/2.jpg',
];

$randomEntry = array_rand($arr, 1);
Darren
  • 76
  • 1
  • 6
0

use this without the loop

$key = array_rand($arr);
echo $arr[$key];

full example

$arr = [
   'chleb' => 'skiny/1.jpg',
   'mienso' => 'skiny/2.jpg',
   'mienso2' => 'skiny/3.jpg',
   'mienso3' => 'skiny/4.jpg',
   'mienso4' => 'skiny/5.jpg',
   'mienso5' => 'skiny/6.jpg',
   'Hasasdasd' => 'skiny/7.jpg',
 ];

$key = array_rand($arr);
echo $key;
echo $arr[$key];
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
0

Are the '.jpg' files meant to all be the same except for one? Because randomly choosing between 7 files when 6 are the same is going to return the same file more than often.

$rand_keys = array_rand($arr);
echo $arr[$rand_keys];
Andreas
  • 23,610
  • 6
  • 30
  • 62
Cambray
  • 134
  • 3
  • 14
0

You should print array result like below,

$rand_keys = array_rand($arr, 1);
echo $arr[$rand_keys[0]] . "\n";
KMS
  • 566
  • 4
  • 16
0
<?php
    $input = array(
    'chleb' => 'skiny/1.jpg',
    'mienso' => 'skiny/2.jpg',
    'mienso2' => 'skiny/3.jpg',
    'mienso3' => 'skiny/4.jpg',
    'mienso4' => 'skiny/5.jpg',
    'mienso5' => 'skiny/6.jpg',
    'Hasasdasd' => 'skiny/7.jpg',
);

foreach($input as $key => $value) {
    $keys = array_rand( $input, 1);
    echo $input[$keys];
}
?>
Alpha_Bords
  • 425
  • 1
  • 4
  • 11