Re: re-keying an array
| From: | John W. Holmes | Date: | Fri, 30 Apr 2004 03:34:50 +0000 |
| Subject: | Re: re-keying an array | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184996@lists.php.net to get a copy of this message | ||
Justin French wrote:
On 30/04/2004, at 12:06 PM, John W. Holmes wrote:If I'm following what you want to do correctly: for($i=0;$i<2;$i++) { $key = array_rand($filenames); $files[] = "path/to/".$filenames[$key]; unset($filenames[$key]); } -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.comAlthough this begs the question of why you'd need to "rekey" an array...It seemed the easiest way to get the first two elements off the beginning of the array after shuffle()ing it, or so I thought -- but I'm more than happy to hear of a better way, as always :) My only requirement is really to end up with an array named $files that can be used later on in the template in a foreach loop. <?shuffle($filenames); // random sort $filenames = array_values($filenames); // re-keyfor($i=0;$i<2;$i++) {$files[] = "path/to/".$filenames[$i]; }?> ... then in the template: <? foreach($files as $file): ?><div ...> ... <? include($file); ?> ... </div><? endforeach; ?> Like I said, always happy to see something in a few less lines :)