PhotoDateChanger
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app

How to Get a Random Item from an Array in PHP

       

Use this snippet for grabbing a random item from an array in php

    $fruits = ['apple', 'banana', 'carrot', 'date', 'elderberry'];
    echo array_rand(array_flip($fruits)); 
    // => 'banana'

PHP's array_flip makes the keys the values and the values the keys.

array_flip(['apple', 'banana', 'carrot', 'date', 'elderberry']);
=> [
 "apple" => 0,
 "banana" => 1,
 "carrot" => 2,
 "date" => 3,
 "elderberry" => 4,
    ]

You could also use shuffle but that will change the order of the original array.

shuffle($fruits);
echo $fruits[0];
// but fruits is out of order now :(
[
 "carrot",
 "apple",
 "elderberry",
 "date",
 "banana",
]
Tagged w/ #php #array #random #snippet #shuffle