Page 1 of 1

ARRAYS .. ¤#!"! stuck for 3 days now ...

Posted: Sat Nov 30, 2002 3:15 pm
by soren
I hope someone can help me here ... i've struggled with this for 3 days now ...

I am making a dynamic website, where I want a textfile (panel_definitions.txt) to form the basis for dynamically created hyperlinks. Currently the textfiles looks like this:

PANEL:p01
Generelt om DKF|p02:dkf01:f01
Faglig information|p03:fag01:f01
Patient information|p04:pat01:f01
Uddannelse|p05:udd01:f01
Links til www|p06:lin01:f01
PANEL:p02
Bestyrelsen:p02:dkf02:f01

This defines 2 panel with 5 and 1 hyperlinks respectively - how I turn the gobbledygook into a link is not important for now.

What I want is a php script to changes this text file into an array. Specifically the array $panel is associative and would consist of two elements called p01 and p02 (see text file above), those elements are themselves array and would consist of 5 and 1 elements respectively, NOT named, just indexed. I have written this script:

<?php
$file_into_array = file("panel_definitions.txt"); // load the text file into an array
$number_lines=count($file_into_array);
$panel = array();

$counter=0;
do {
$current_line=$file_into_array[$counter];
// each line in the text file defines EITHER a panel OR an element in the panel

if (substr($current_line,0,6)=="PANEL:") { // The current line defines a panel
$panel_name=substr($current_line,6,strlen($current_line)-1); // Extract the panel name (after "PANEL:")
$panel[$panel_name] = array(); // Create a 'named' element in the panel-array, let that element be an array itself

}

if (substr($current_line,0,6)<>"PANEL:") { // The current line defines an element i the 'current' panel
$panel[$panel_name][] = $current_line; // Create an element in the array-within-an-array and let it be the current text line

}
$counter++; // move to the next line
} while ($counter<$number_lines);

echo count($panel); // This outputs 2 as expected
echo count($panel['p01']); // I would expect this to output 5, but it outputs 0 instead >:(

?>

The script runs without error, It creates the $panel array, which correctly contains 2 elements, but the indexed 'sub-arrays' are empty ... I am completely clueless ...

Soren O'Neill

Posted: Sat Nov 30, 2002 3:42 pm
by MeOnTheW3
Try the foreach() and switch() functions:

Code: Select all

&lt;?php
$file_into_array = file("panel_definitions.txt"); // load the text file into an array 
$number_lines=count($file_into_array); 
$panel = array();
$current_panel = '';

foreach($file_into_array AS $index =&gt; $line)
{
   $lineVals = explode(':',$line);

   switch($lineVals&#1111;0])
   {
         case 'PANEL':
                 $current_panel=$lineVals&#1111;1];
                 if(is_array($panel&#1111;$current_panel])) // make sure it is not being reset
                 {
                    echo 'Error: Panel ' . $lineVals&#1111;1] . ' is being reset, check file for duplicate entries.';
                 }else{
                    $panel&#1111;$current_panel] = array;
                 }
                break;

         default:
                 $link = explode('|',$lineVals&#1111;0]);
                 $panel&#1111;$current_panel]&#1111;'link_name'] = $link&#1111;0];
                 $panel&#1111;$current_panel]&#1111;'link_uri'] = $link&#1111;1];
                 $panel&#1111;$current_panel]&#1111;'3rd_value'] = $lineVals&#1111;2];
                 $panel&#1111;$current_panel]&#1111;'4th_value'] = $lineVals&#1111;3];
                break;
   }

}
?&gt;

Did this help?

Probelm solved ..

Posted: Sun Dec 01, 2002 12:49 pm
by soren
Hi .. no Im afraid it didnt help - it wasnt the loop construction which was bugging me ...

The solution to the problem was to change

$current_line=$file_into_array[$counter];

into

$current_line=trim($file_into_array[$counter]);

I supose the .txt file contains [new-line] binary info or something .. anyway, it did the trick.

Thanks just the same though ..
Soren O'Neill