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
ARRAYS .. ¤#!"! stuck for 3 days now ...
Moderator: General Moderators
Try the foreach() and switch() functions:
Did this help?
Code: Select all
<?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 => $line)
{
$lineVals = explode(':',$line);
switch($lineValsї0])
{
case 'PANEL':
$current_panel=$lineValsї1];
if(is_array($panelї$current_panel])) // make sure it is not being reset
{
echo 'Error: Panel ' . $lineValsї1] . ' is being reset, check file for duplicate entries.';
}else{
$panelї$current_panel] = array;
}
break;
default:
$link = explode('|',$lineValsї0]);
$panelї$current_panel]ї'link_name'] = $linkї0];
$panelї$current_panel]ї'link_uri'] = $linkї1];
$panelї$current_panel]ї'3rd_value'] = $lineValsї2];
$panelї$current_panel]ї'4th_value'] = $lineValsї3];
break;
}
}
?>Did this help?
Probelm solved ..
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
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