0

I have this simple line:

       $images = glob($directory . "*.html");

It returns a list of files like this:
e.g 1

17001400300120110004600.html
17001400300120110004700.html
17001400300120110004800.html
17001400300120110004900.html
17001400300120110005000.html

The problem is that I don't need an ordered list. I need a random list, like this:

e.g 2

17001400300120110004700.html
17001400300120110005000.html
17001400300120110004900.html
17001400300120110004600.html
17001400300120110005800.html

I've tried with NOSORT ( $images = glob($directory . "*.html", GLOB_NOSORT); ) flag but returns an ordered list like in the first example.

How can I get a random list?

2
  • 1
    Just do something like shuffle($images); Commented Dec 29, 2012 at 9:08
  • 1
    The reason they're sorted even when you use NOSORT is because it uses the order they exist in the directory, which is often the order the files were created, and they were probably created in numerical order. Commented Dec 29, 2012 at 9:10

2 Answers 2

7

Use shuffle on array returned from glob.

Using nosort won't make your array random, it will just read them in order they appear in the directory instead of sorting them by name, as documentation states:

GLOB_NOSORT - Return files as they appear in the directory (no sorting)

Have in mind that shuffle takes array as referrence so you will need to do just this:

$images = glob($directory . "*.html");
shuffle($images);
Sign up to request clarification or add additional context in comments.

Comments

-1
$images = shuffle(glob($directory . "*.html"));

1 Comment

shuffle returns a boolean as can be read in the documentation. Therefore this won't work

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.