Jump to content

Arrays and Foreach


phpdolan

Recommended Posts

I've spent toooo many hours the last few days on this, tried many methods, read many pages.... Now requesting help.

 

I have a multi-dimensionsal array in the form of a 6 x 10 table. I have 2 problems:

 

1) The foreach statement that inputs each row into a table, and

 

2) The php syntax in the table.

 

First, I'm still building, so the input is in the form of a string that I 'extract' data from. There are 7 input strings in the following form:

 

$line[0] = "00PDMIA737B3071110852006040411000000000....540351210500000000000000000";

$line[1] = "01MIAIAH092017102518068502 0165B80....0000000000002250000 0000000000000";

 

(Here's a snippet of my code)

 

for ( $l=0 ; $l<=6 ; $l++ )

{

  if ($l == 0)

    {

      $leg[$l] = substr($line[$l],0,2);

      $seq_no[$l] = substr($line[$l],16,5);

      $total_fly[$l] = substr($line[$l],46,4);

        .....

    }

    else

    {

      $leg[$l] = substr($line[$l],0,2);

      $dep[$l] = substr($line[$l],2,3);

      $dep_minutes[$l] = substr($line[$l],8,4);

      $dep_time[$l] = intval($dep_minutes[$l]/60) . ':' . $dep_minutes[$l] % 60 ;

      $arr[$l] = substr($line[$l],5,3);

        ......

 

I have confirmed that data IS in the array with a var_dump ($leg, $seq_no, $dep, ....)

 

After converting some of the data to an hr:mn format, I've been trying to put in in different table cells with a foreach ( ) statement. Please keep in mind that the table will be 6 x 10, and I know that the code below is incorrect.

 

for ( $leg=1 ; $leg=6 ; $leg++ )

{

echo "<tr><td>".$line['leg']."</td><td>".$line['dep']."</td></tr><td>".$line['dep_time']."</td><td>".$line['arr']."</td><td>";

  .....

 

My results are either 1) infinite loop or 2) cells with outline, but blank. Output looks correct if I only include code for the first column which is the $leg array value.

 

Please make suggestions. I am not clear with arrays (obviously). I hope I've provided enough code.

 

Thanks,

David

 

Link to comment
https://forums.phpfreaks.com/topic/75391-arrays-and-foreach/
Share on other sites

You don't want to be indexing into the $line variable, you want to reference the individual arrays:

<?php
for ($i=0;$i<6;$i++) {
   echo '<tr><td>' . $leg[$i] . '</td><td>' . $dep[$i] . '</td><td>' . $dep_time[$i] . '</td><td>' . $arr[$i] . '</td><td>';
}?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/75391-arrays-and-foreach/#findComment-381361
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.