Re: Array Question

From: Date: Tue, 18 Jul 2000 16:58:55 +0000
Subject: Re: Array Question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-7099@lists.php.net to get a copy of this message
On Tue, 18 Jul 2000, Phillip S. Baker wrote: > Okay I want to populate an array with a variable (i think that is the > way to state it) > > Ok ay I am using this code > > $d = dir("images/"); > while($entry=$d->read()) > { > if(($entry==".") OR ($entry=="..")) > { > echo ""; > } > else > { > echo $entry, "\n"; > } > } > $d->close(); <snip> > How would I go about doing this in PHP 3 not PHP 4?? There really aren't very many version incompatibilities between PHP3 and PHP4. This will work in either case. Since you can't predict how many images will be in the directory, or what their names will be, building the array in one step won't work unless you wanted to build up a delimited string and implode() it afterwards, which is just extra work. Fortunately, you can append to an array in a loop, which makes the task you want to accomplish quite simple. $d = dir('images/'); while( $entry = $d->read() ) { if( ($entry != '.') AND ($entry != '..') ) $array[] = $entry; } $d->close(); When it finishes, you'll have $array with numerically keyed elements for each file in the directory. Matt

« previous php.general (#7099) next »