Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi-Dimensional Arrays 1

Status
Not open for further replies.

DreamerZ

Programmer
Jul 11, 2001
254
US
Okay, I'm lost. Here's what I have:

I have a list of values in a text file. What I need to do is combine like values on an HTML page.

For example (text file data):
Line 1 = a^12^Yes^6/21/04
Line 2 = b^1^No^5/14/04
Line 3 = a^5^No^6/1/04

I want the output to be like:
A 12,5 Yes,No 6/21/04,6/1/04
B 1 No 5/14/04

Here's the code currently:
Code:
$num=1;
$i=0;
$names=array();
$values=array();
if(!($fp_openAbsence = @fopen ("openAbsences.txt", "r"))) {
  echo "No Active Absences\n
  <script>document.getElementById('close_Abs').disabled=true</script>";
  exit; }
while (!feof($fp_openAbsence)) {
  $open_abs = fgets($fp_openAbsence);
  $fields = @explode("^",$open_abs);
  if ($fields[0]=="\n") { unset($fields[0]); }
  else {
    if ($fields[0] != "") {
	if(!in_array($fields[0],$names))
	{
	  array_push($names,$fields[0]);
	  $values=array($names[$i]=>$names[$i]);
	  array_push($values,$fields[1].$fields[2]);
	  echo "Name array-$i: $names[$i]<BR>";
	  echo "Fields array-$i:"; print_r($values); echo "<BR>";
	}
	else
	{
	  array_push($values,$fields[1].$fields[2]);
	  echo "Values: "; print_r($values); echo "<BR>";
	  echo "$fields[0] already in Names array<BR>";
	}
echo "Array data:"; print_r($values[0]);
Basically, what I want to do is have an array ($names) with multiple keys if the $fields[0] is already in $names array.

So:
Array(a[0]=>12^Yes^6/21/04 a[1]=>5^No^6/1/04)
Array(b[0]=>1^No^5/14/04)

I can then manipulate the print to the HTML page to show what I need.

I am confused on how to combine the like data, if that helps in the summary of what I'm trying to do.

Is there a better way or is the multi-dimensional array creation the way to go. I'm open to anything at this point.

Thx,

DreamerZ
 
I don't know if this is what you're looking for or not, but the script:

Code:
<?php
print '<html><body>';

$absences_filename = 'test_absences.txt';

if (file_exists($absences_filename))
{
	$fh = fopen ($absences_filename, 'r');

	$absence_data = array();

	if ($fh !== FALSE)
	{
		while ($absence_line = fgets($fh))
		{
			$absence_array = explode('^', $absence_line);
			
			$absence_data[strtoupper($absence_array[0])][] = array
											(
												'some_number' => $absence_array[1],
												'some_yesno_value' => $absence_array[2],
												'some_date' => $absence_array[3]
											);
		}
	
		print '<pre>';
		print_r($absence_data);
		print '</pre>';
	}
	else
	{
		print 'Could not open datafile';
	}
}
else
{
	print 'Input file not found';
}

print '</body></html>';
?>

Using the input file:

Code:
a^12^Yes^6/21/04
b^1^No^5/14/04
a^5^No^6/1/04

Produces:

Code:
Array
(
    [A] => Array
        (
            [0] => Array
                (
                    [some_number] => 12
                    [some_yesno_value] => Yes
                    [a_date] => 6/21/04

                )

            [1] => Array
                (
                    [some_number] => 5
                    [some_yesno_value] => No
                    [a_date] => 6/1/04
                )

        )

    [B] => Array
        (
            [0] => Array
                (
                    [some_number] => 1
                    [some_yesno_value] => No
                    [a_date] => 5/14/04

                )

        )

)





Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That works perfectly. I thought, however, I'd be able to display the results on the page.

How do I access each individual variable if output as above?

Again, I want to end up with something like:
A 12,5 Yes,No 6/21/04,6/1/04
B 1 No 5/14/04

in table format
<td>A</td><td>12,5</td>...
<td>B</td><td>1</td>...

Thank you for the assistance. I am a little weak on arrays.

DreamerZ

DreamerZ
 
if you wanted, for instance to use the value for 12, you could do:
$array[A][0][some_number]

___________________________________
[morse]--... ...--[/morse], Eric.
 
The problem with $array[A][0][some_number] is that I don't know the actual values.

I don't know at the outset that A is there, nor how many A's I have.

So I would need something like:
$array[$absence_array[0]][$i][some_number]

DreamerZ

DreamerZ
 
Well, in an associative array, I dont believe you can have keys of the same name, they just get pushed on to the existing array.

___________________________________
[morse]--... ...--[/morse], Eric.
 
I'm in a bit of a rush, so I'll admit upfront that I didn't read the whole post... below are some samples I think may help you though...
Code:
foreach ($array1 as $key=>$subinfo) { 
  foreach ($subinfo as $moreinfo) {
    echo $key.','.$moreinfo;
  }
}

$my_array_keys=array_keys($array1);

Hope it helps.
 
DreamerZ:
This function:

Code:
function print_array_as_table($the_absence_array)
{
	print '<table border="1">';
	foreach ($the_absence_array as $key => $data)
	{
		$the_count = count($data);
		print '<tr>';
		print '	<td rowspan="' . $the_count . '">' . $key . '</td>';
		
		$counter = 1;
		foreach ($data as $row)
		{
			print '<td>' . $row['some_number'] . '</td><td>' . $row['some_yesno_value'] . '</td><td>' . $row['some_date'] . '</td>'; 
			
			if ($counter < $the_count)
			{
				print '</tr><tr>';
			}
		}
		print '</tr>';
	}
	print '</table>';
}

Will output the datastructure my earlier code created in a table.

Just add that function definition to the end of the script and replace:

Code:
       print '<pre>';
        print_r($absence_data);
        print '</pre>';

with

Code:
		print_array_as_table ($absence_data);



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thank you very much for the information. As I said, I'm a little weak on arrays.

I will be working through some other issues, like adding checkboxes to each row as well as hyperlinks to other pages based on the results of the fgets.


DreamerZ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top