# Multi dimension arrays

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi,

This is probably a no-brainer, but I could do with some help here.

I have some code which looks like this

\$event = array("Date"=>array(), "Time"=>array(), "Venue"=>array(),
"Event"=>array());
\$line = array();

while (!feof(\$fp))
{
\$datein = fgets(\$fp, 200);
\$lineleft = \$datein;
\$newpos = 0;
\$pos = 0;
for (\$i = 0; \$i < 3; ++\$i)
{
\$newpos += strpos(\$lineleft, "*");
array_push(\$line, substr(\$datein, \$pos, \$newpos));
\$newpos++;
\$lineleft = substr(\$datein, \$newpos, strlen(\$datein) - \$newpos);
\$pos = \$newpos;
}
\$month = substr(\$line[0], 5, 2);
\$day = substr(\$line[0], 8, 2);
\$year = substr(\$line[0], 0, 4);
if (\$year >= \$today['year'])
{
if (\$month >= \$today['mon'])
{
if ((\$month == \$today['mon']) && (\$day <= \$today['mday']))
continue;
\$datet = \$day ."-" .\$month."-".\$year;
array_push(\$event['Date'], \$datet);
}
}
array_push(\$event['Time'], \$line[1]);
array_push(\$event['Venue'], \$line[2]);
array_push(\$event['Event'], \$line[3]);
}
fclose (\$fp);

I know the substr stuff works (I can test that and it's fine and dandy).
I know the \$month et al works as well (I've cut and pasted it from a
website I run at work). However, I'm not at all sure that how I'm using
the empty arrays (especially the multi-dimension array) is correct.

Could someone give me some advice on this?

Thanks

TTFN

Paul
--
"Logic, my dear Zoe, is merely the ability to be wrong with authority" -
Dr Who

## Re: Multi dimension arrays

Paul wrote:

[snip]

Small oops, your loop only runs three times, but later you expect \$line to
contain 4 values. Unless you have some special plan with the code above,
you could get the same result by just using:
\$line = split("\*", \$datein);

[snip]

In some conditions \$event['Date'] does *not* get an value, while the others
(Time, Venue, Event) do. For instance if \$year < \$today['year']. I assume
it is not what you intended.

[snip]

Use of array_push looks fine.
You could use syntax like \$event['Event'][] = \$line[3], but the result would
be the same.

/Bent

## Re: Multi dimension arrays

Bent Stigsen wrote:

Thanks :-)

My main problem though is how to get data out of the array.

For example

echo \$event['Event'][\$i] . "<br />";

doesn't give anything - even if I have explicity put something into the
array (for example

\$event['Event'][0] = "wibble";
\$event['Event'][1] = "wobble";
\$event['Event'][2] = "fibble";
\$event['Event'][3] = "fobble";

for (\$i = 0; \$i < 4; ++\$i)
echo \$event['Event'][\$i] . " ... " . \$i . "<br />";

displays nothing other than " ... 0-3")

Is this more likely to be me or the version of php I'm using?

TTFN

Paul

## Re: Multi dimension arrays

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

## Re: Multi dimension arrays

Paul F. Johnson wrote:
[snip]

I've tried your example verbatim and output is fine, so I reckon it is