You are here: Re: Array troubles « All PHP « IT news, forums, messages
Re: Array troubles

Posted by Jerry Stuckle on 08/24/07 01:00

Kyote 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)] = 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?
>
> ---
> Kyote

$books = array();
foreach ($files as $value) {
$c = substr($value,0,1);
if (!is_array($books[$c]))
$books[$c] = array();
$books[$c][] = $value;
}


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация