Title: PHP Array Question
Last modified: August 19, 2016

---

# PHP Array Question

 *  Resolved [kasperbs](https://wordpress.org/support/users/kasperbs/)
 * (@kasperbs)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/php-array-question/)
 * I hope it’s ok to post PHP specific threads in here. If anyone know of a good
   online forum for a beginning php’er I would be happy to know.
 * Anyway, I want to go through a specific directory and locate all files that ends
   on ‘-thumb.png’ and load them into an array from which I then can select two 
   random images to display independently.
 * Thanks in advance

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/php-array-question/#post-948310)
 * This search:
    [http://www.google.com/search?q=php+array+of+file+names](http://www.google.com/search?q=php+array+of+file+names)
 * found, among others, this:
    [http://forums.techguy.org/web-design-development/545077-php-array.html](http://forums.techguy.org/web-design-development/545077-php-array.html)
 * `$files = glob('*.php'); // glob() returns an array of filenames matching the
   extension`
 * Resource:
    [http://us3.php.net/glob](http://us3.php.net/glob) [http://us3.php.net](http://us3.php.net)
 *  Thread Starter [kasperbs](https://wordpress.org/support/users/kasperbs/)
 * (@kasperbs)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/php-array-question/#post-948391)
 * Thanks MichaelH, I’m looking into it now.
 *  Thread Starter [kasperbs](https://wordpress.org/support/users/kasperbs/)
 * (@kasperbs)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/php-array-question/#post-948394)
 * Thanks for your help, I finally figured out how to do it. I struggled with the
   path to the directory, I tried relative paths, absolutes paths but couldn’t figure
   out the right way to do it. In the end I came up with the right thing and the
   following does exactly what I asked in my original question. So if anyone is 
   interested in the same thing, here goes:
 *     ```
       <?php
       $filenames = glob('wp-content/uploads/portfolio/*-thumb.png');
         $rand_files = array_rand($filenames,2);
         $thumb1 = $filenames [$rand_files[0]];
         $thumb2 = $filenames [$rand_files[1]];
       ?>
       ```
   
 * This will put all the files with a ‘-thumb.php’ extension from the portfolio 
   folder, into an array. It will select two random items and put them into two 
   variables. Then I just echo $thumb1 or $thumb2 to display the source of the image.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘PHP Array Question’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [kasperbs](https://wordpress.org/support/users/kasperbs/)
 * Last activity: [17 years, 4 months ago](https://wordpress.org/support/topic/php-array-question/#post-948394)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
