Multi dimension arrays

This is a discussion on Multi dimension arrays within the PHP Language forums, part of the PHP Programming Forums category; Hi, This is probably a no-brainer, but I could do with some help here. I have some code which ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-19-2006
Paul
 
Posts: n/a
Default Multi dimension arrays

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

Reply With Quote
  #2 (permalink)  
Old 05-19-2006
Bent Stigsen
 
Posts: n/a
Default Re: Multi dimension arrays

Paul wrote:

[snip]
> 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;
> }


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]
> 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]);


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]
> 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.


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


/Bent
Reply With Quote
  #3 (permalink)  
Old 05-21-2006
Paul F. Johnson
 
Posts: n/a
Default Re: Multi dimension arrays

Bent Stigsen wrote:

> Paul wrote:


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

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


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
Reply With Quote
  #4 (permalink)  
Old 05-21-2006
Kevin Wells
 
Posts: n/a
Default Re: Multi dimension arrays

In message <zi_bg.181096$xt.49656@fe3.news.blueyonder.co.uk >
"Paul F. Johnson" <paul@all-the-johnsons.co.uk> wrote:

>Bent Stigsen wrote:
>
>> Paul wrote:

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

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

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


Have you tried [Event(1)]
--
Kev Wells http://kevsoft.topcities.com
http://kevsoft.co.uk/
ICQ 238580561
The older I get the faster I used to be.
Reply With Quote
  #5 (permalink)  
Old 05-23-2006
Bent Stigsen
 
Posts: n/a
Default Re: Multi dimension arrays

Paul F. Johnson wrote:
[snip]
> 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?


I've tried your example verbatim and output is fine, so I reckon it is
not your code.
I have run the same code on php versions from different major releases
(3.0.17, 4.3.9 and 5.1.2) giving exact same result, so I think it is
safe to say that it is not an issue of php version.

Sorry, I can't really say what your problem is. Perhaps your
braille-display is broken.

/Bent
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:41 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0