|  | Posted by Rik on 08/24/07 01:03 
On Fri, 24 Aug 2007 02:23:10 +0200, Kyote <kyote_love@nospamhotmail.com>==
 
 wrote:
 
 > I'm rather new to php.
 >
 > I have an array of filenames named $files. I'm wanting to iterate
 > through the array and take the first letter of each string in the
 > array and make a new array with the keys as that first letter, then
 > assign the filename to the lettered key of the new array. I've tried
 >
 > foreach ($files as $value){
 > 	$books[substr($value,0,1)] =3D array($value);
 > }
 >
 > But I'm not getting quite what I'm trying for. It keeps replacing the
 > value for each key instead of adding to it like I'm trying for.
 >
 > $books["A"]
 >
 > The string assigned to the above keeps being replaced by the new
 > string that starts with "A". I want it to add the new string that
 > starts with "A" to the last one added. I'm hoping there's just
 > something simple I'm overlooking. Can anyone help me?
 
 
 //initialise books
 $books =3D array();
 foreach($files as $value){
 //get first character
 $first_char =3D $value[0];
 //initialise array if not set (not strictly necessary....
 if(!isset($books[$first_char])) $books[$first_char] =3D array();
 //add value to books
 $books[$first_char][] =3D $value;
 }
 -- =
 
 Rik Wasmus
  Navigation: [Reply to this message] |