Multi dimension arrays

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

Threaded View

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(),
$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));
    $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']))
      $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?



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

Re: Multi dimension arrays

Paul wrote:

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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


Re: Multi dimension arrays

Bent Stigsen wrote:

Quoted text here. Click to load it

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?



Re: Multi dimension arrays

Kimmo Laine wrote:
Quoted text here. Click to load it

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

Quoted text here. Click to load it


    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:
Quoted text here. Click to load it

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.


Site Timeline