How to put this infor into a multi dimensional array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

How to put this infor into a multi dimensional array?

Post by slipstream »

ok, here is the situation I read in a file that has multiple data per line. I would like to put it into a multi dimensional array. Here is how I do it now so you can see.

foreach($file as $line)
{
$info = explode('|', $line);

echo " <tr>\n";
echo " <td>" . $info[0] . "</td>";
echo " <td>" . $info[1] . "</td>";
echo " <td>" . $info[2] . "</td>";
echo " <td>" . $info[3] . "</td>";
echo " </tr>\n";

$totHours += $info[2];
}

the $info array keeps 4 pieces of data from each line and spits them out. I still want to output them to the screen but I want it in an array so I know which line each one is on. Hope it makes sense...
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

are there any standard characters between the pieces of information, or could you add them? If so, you can use a list in conjunction with split, explode, or preg_split

Code: Select all

<?php
$i=0;
$info=array();
foreach($file as $line){
  $info[$i]=array();
  $info[$i]=explode("|",$line);
  $i++;
}

for($j=0;$j<$i;$j++){
  echo " <tr>\n";
  echo " <td>" . $info[$j][0] . "</td>";
  echo " <td>" . $info[$j][1] . "</td>";
  echo " <td>" . $info[$j][2] . "</td>";
  echo " <td>" . $info[$j][3] . "</td>";
  echo " </tr>\n";
}
?>
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

great that worked, thanks.

Now I am attempting to put a button on each line saying "delete" and if they press it I will remove that item from the array. How the heck do I set it up to do that?
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

anyone? Here is how I set the buttons up:

$i=0;
$info=array();
foreach($file as $line){
$info[$i]=array();
$info[$i]=explode("|",$line);
$i++;
}

for($j=0;$j<$i;$j++){
echo " <tr>\n";
echo " <td>" . $info[$j][0] . "</td>";
echo " <td>" . $info[$j][1] . "</td>";
echo " <td>" . $info[$j][2] . "</td>";
echo " <td>" . $info[$j][3] . "</td>";
echo ' <td><input type="submit" value="Delete Entry"></td>'; //The delete button
echo " </tr>\n";
$totHours += $info[$j][2];
}
Post Reply