ARRAYS .. ¤#!"! stuck for 3 days now ...
Posted: Sat Nov 30, 2002 3:15 pm
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
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