Random/Shuffle Images in PHP

shuffle function in php

Hello friends, Sometimes we need to show random images on your page on every refresh. user will see random images. suppose if we have blog that have 50 images using php shuffle function we will show random images on our home page.

shuffle() function randomizes the order of the elements in the array.shuffle function assigns new keys for the elements in the array. Existing keys will be remove by shuffle function and assign new key” . One  drawback of using shuffle()  is that it remove your array keys. This is unavoidable sadly, and you need to live with it when you use shuffle().  that shuffle uses parameter by reference  or  it returns true, but shuffles up the parameter that you passed it.

shuffle on simple array :


Shuffle an associative array :

we will use shuffle an associative array  function  after using this function the array will be randomized but key will be dropped. its take a more work but we will randomize the assciative array by shuffling the array key and again reassembling the array.

Example :


Now every time array is shuffle with keys. if you want to check if value exist in php array

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

76 + = 85

%d bloggers like this: