0

I am trying to put a load of images in a random order using php this is what I have tried

for ($i=47; $i < 54; $i++) {
            echo "<img width='205' src='IMG_03";
            echo rand(47,53);
            echo ".jpeg' alt=''>";  
}

But this code includes duplicates how could I do this without having the same random number between 47 and 53 twice?

1
  • $values = range(47,53); shuffle($values); Commented Jun 5, 2014 at 21:58

3 Answers 3

1
<?php

$numbers = range(47, 54);
shuffle($numbers);

foreach($numbers as $n) {
    echo '<img src="IMG_03'.$n.'.jpg" alt="" />';
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$array = range(47, 53);
shuffle($array);

foreach ($array as $a) {
    echo "<img width='205' src='IMG_03";
    echo $a;
    echo ".jpeg' alt=''>";  
}

range() creates an array of numbers between the given values: 47, 48, ..., 53

shuffle() puts it in random order

Comments

0

Use the shuffle method

$arr = array(47, 48, 49, 50, 51, 52, 53);

shuffle($arr);

while($element = array_pop($arr)){
    echo "<img width='205' src='IMG_03";
    echo $element;
    echo ".jpeg' alt=''>";  
}

Comments

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.