3

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 ?

3

7 Answers 7

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I need to have got value and key not only .jpg file.
@Season6 See edit. Is that what you are looking for?
Yes ! Thanks very much :D
0

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

4 Comments

Notice: Array to string conversion in E:\xampp\htdocs\csgolos\freeCase.php on line 65
$a = array_rand($arr,1); echo $a[0];
Then this only echo the first letter of the key if we have got chleb then the echo gives - c
$a = array_rand($arr,1); echo $a. It's working for me
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);

Comments

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];

Comments

0

You should print array result like below,

$rand_keys = array_rand($arr, 1);
echo $arr[$rand_keys[0]] . "\n";

Comments

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];

Comments

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];
}
?>

1 Comment

or change $input to $arr

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.