Array troubles

Array troubles

am 24.08.2007 02:23:10 von Kyote

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

Re: Array troubles

am 24.08.2007 03:00:14 von Jerry Stuckle

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
==================

Re: Array troubles

am 24.08.2007 03:03:24 von luiheidsgoeroe

On Fri, 24 Aug 2007 02:23:10 +0200, 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)] =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

Re: Array troubles

am 24.08.2007 03:16:17 von Kyote

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

Thank you very much Jerry. This seems to work exactly as I need.

---
Kyote

Re: Array troubles

am 24.08.2007 03:19:53 von Kyote

Thank you for the help Rik. Looks like this is almost the same as
Jerry. It's fantastic to see an alternate way to do it.

---
Kyote

Re: Array troubles

am 24.08.2007 03:25:09 von luiheidsgoeroe

On Fri, 24 Aug 2007 03:19:53 +0200, Kyote
wrote:

> Thank you for the help Rik. Looks like this is almost the same as
> Jerry. It's fantastic to see an alternate way to do it.

Well, it aint that alternate :P.

> ---
> Kyote

BTW: your sig seperator is broken. It should be "-- \n"
(dash)(dash)(space)(newline)

--
Rik Wasmus

Re: Array troubles

am 24.08.2007 03:37:22 von Steve

"Kyote" wrote in message
news:96csc3tplg3md00t969hu45f74dt6bb80u@4ax.com...
|
| >$books = array();
| >foreach ($files as $value) {
| > $c = substr($value,0,1);
| > if (!is_array($books[$c]))
| > $books[$c] = array();
| > $books[$c][] = $value;
| >}
|
| Thank you very much Jerry. This seems to work exactly as I need.

jerry's got it...but just a tid-bit of fun...

a string is an array of characters. so, $c could easily be set by:

$c = $value[0];

Re: Array troubles

am 24.08.2007 03:44:34 von Jerry Stuckle

Steve wrote:
> "Kyote" wrote in message
> news:96csc3tplg3md00t969hu45f74dt6bb80u@4ax.com...
> |
> | >$books = array();
> | >foreach ($files as $value) {
> | > $c = substr($value,0,1);
> | > if (!is_array($books[$c]))
> | > $books[$c] = array();
> | > $books[$c][] = $value;
> | >}
> |
> | Thank you very much Jerry. This seems to work exactly as I need.
>
> jerry's got it...but just a tid-bit of fun...
>
> a string is an array of characters. so, $c could easily be set by:
>
> $c = $value[0];
>
>

That's true - just trying to keep it close to what he had. Didn't want
to cornfuse him too much :-)

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

Re: Array troubles

am 24.08.2007 06:05:13 von Steve

"Jerry Stuckle" wrote in message
news:lPudnec_iOnQpVPbnZ2dnUVZ_o7inZ2d@comcast.com...
| Steve wrote:
| > "Kyote" wrote in message
| > news:96csc3tplg3md00t969hu45f74dt6bb80u@4ax.com...
| > |
| > | >$books = array();
| > | >foreach ($files as $value) {
| > | > $c = substr($value,0,1);
| > | > if (!is_array($books[$c]))
| > | > $books[$c] = array();
| > | > $books[$c][] = $value;
| > | >}
| > |
| > | Thank you very much Jerry. This seems to work exactly as I need.
| >
| > jerry's got it...but just a tid-bit of fun...
| >
| > a string is an array of characters. so, $c could easily be set by:
| >
| > $c = $value[0];
| >
| >
|
| That's true - just trying to keep it close to what he had. Didn't want
| to cornfuse him too much :-)

i know...it's one of those 'i never knew that' things. since you covered the
answer just fine, i thought i'd give the 'one to grow on'. after i posted
that, i was overjoyed to see rik had actually done the same thing in his
example.

hey, bet you two bits the op is processing flat files and the first
character of each line is the record type. ;^)