How to read file into keyed array? | | |
I have a text file ("eighty.txt") that looks like this:
83|84|85|86
I can read the file into an array like this:
$numbers= file("eighty.txt");
But how do I key the array? I'd like to use strings as keys: three, four, five,
six - do I add the key names in the file?
The reason I need keys is because I will need to open the file and change one or
more of the key elements every so often. So, I assume I would read the file
into an array, reference the key, and change the value for that key?
suggestions/examples welcome...
Thanks! | | | | re: How to read file into keyed array?
[color=blue]
>"deko" <dje422@hotmail.com> schrieb im Newsbeitrag[/color]
news:ruY6c.40742$4E3.21593@newssvr25.news.prodigy. com...[color=blue]
> I have a text file ("eighty.txt") that looks like this:
>
> 83|84|85|86
>
> I can read the file into an array like this:
>
> $numbers= file("eighty.txt");
>
> But how do I key the array? I'd like to use strings as keys: three, four,[/color]
five,[color=blue]
> six - do I add the key names in the file?
>
> The reason I need keys is because I will need to open the file and change[/color]
one or[color=blue]
> more of the key elements every so often. So, I assume I would read the[/color]
file[color=blue]
> into an array, reference the key, and change the value for that key?
>
> suggestions/examples welcome...
>
> Thanks!
>
>
>[/color]
Hi!
Look at the following code:
$lines=file('eighty.txt');
$numbers=explode('|',$lines[0]);
$numbers[0]=11; //change any number
$output=implode('|',$numbers); //create a string (numbers seperated by | )
$fhandler=fopen('eighty.txt','w');
fwrite($fhandler,$output);
fclose($fhandler);
1. First, the file is read into the array $lines.
2. Your numbers are in line no 1, seperated by '|'. -> use explode to read
the numbers into an array
3. now you can change the numbers, e.g. $numbers[0] (the first number).
4. finally, you create a string containing all numbers seperated by '|' and
write them into your file.
Erik | | | | re: How to read file into keyed array?
Uzytkownik "deko" <dje422@hotmail.com> napisal w wiadomosci
news:ruY6c.40742$4E3.21593@newssvr25.news.prodigy. com...[color=blue]
> The reason I need keys is because I will need to open the file and change[/color]
one or[color=blue]
> more of the key elements every so often. So, I assume I would read the[/color]
file[color=blue]
> into an array, reference the key, and change the value for that key?[/color]
If the actual format of the file doesn't matter, the easiest thing to do is
to serialize the array and save it to file.
$data = file_get_contents("array.dat");
$a = unserialize($data);
..... modify aray ...
$data = serialize($a);
file_put_contents("array.dat", $data); | | | | re: How to read file into keyed array?
> contents of eighty.txt:[color=blue]
>
> 83|84|85|86
>
> code:
>
> 1 $lines=file('eighty.txt');
> 2 $numbers=explode('|',$lines[0]);
> 3 $numbers[0]=11; //change any number
> 4 $output=implode('|',$numbers); //create a string (numbers seperated by | )
> 5 $fhandler=fopen('eighty.txt','w');
> 6 fwrite($fhandler,$output);
> 7 fclose($fhandler);[/color]
Fantastic! This is exactly what I was looking for - thanks.
A couple of questions:
Let's say I just want to get the value of the second element in the array (84).
Do I still need the ",$lines[0]" bit at the end of line 2? Is this optional, or
necessary, to begin the key index at 0? If it's optional, then my code might
look like this:
$lines=file('eighty.txt');
$numbers=explode('|');
$my_second_number = $numbers[1];
echo "my second number = ".$my_second_number[value]
This assumes the array will be automatically indexed starting at 0. Is this
correct? Also, I'm not opening the file with fopen - I don't need to, if all I
want is the value of $numbers[1], correct? I will not be adding or removing
anything from the file, so I will not have to worry about the the index getting
out of order - is this correct? Again, if ",$lines[0]" is optional in the above
example, then do I really need it when writing to the file? | | | | re: How to read file into keyed array?
"deko" <dje422@hotmail.com> schrieb im Newsbeitrag
news:xc27c.12688$MF.7175@newssvr27.news.prodigy.co m...[color=blue][color=green]
> > contents of eighty.txt:
> >
> > 83|84|85|86
> >
> > code:
> >
> > 1 $lines=file('eighty.txt');
> > 2 $numbers=explode('|',$lines[0]);
> > 3 $numbers[0]=11; //change any number
> > 4 $output=implode('|',$numbers); //create a string (numbers seperated by[/color][/color]
| )[color=blue][color=green]
> > 5 $fhandler=fopen('eighty.txt','w');
> > 6 fwrite($fhandler,$output);
> > 7 fclose($fhandler);[/color]
>
> Fantastic! This is exactly what I was looking for - thanks.
>
> A couple of questions:
>
> Let's say I just want to get the value of the second element in the array[/color]
(84).[color=blue]
> Do I still need the ",$lines[0]" bit at the end of line 2? Is this[/color]
optional, or[color=blue]
> necessary, to begin the key index at 0? If it's optional, then my code[/color]
might[color=blue]
> look like this:
>
> $lines=file('eighty.txt');
> $numbers=explode('|');
> $my_second_number = $numbers[1];
> echo "my second number = ".$my_second_number[value][/color]
explode needs two arguments: the seperator ( in your case '|', and the
string that should be read into an array; in your case, this is the first
row of the textfile ($lines[0] ), so you shouldn't change the first two
rows.
if you want to get the value of the second element, you simply use
$numbers[1], the third $numbers[2] and so on.
your example should look like this:
$lines=file('eighty.txt');
$numbers=explode('|',$lines[0]);
$my_second_number = $numbers[1];
echo "my second number = ".$my_second_number;
[color=blue]
>
> This assumes the array will be automatically indexed starting at 0. Is[/color]
this[color=blue]
> correct?[/color]
yes, the index of arrays usually starts at 0.[color=blue]
> Also, I'm not opening the file with fopen - I don't need to, if all I
> want is the value of $numbers[1], correct?[/color]
you could also use fopen(), fread(), ... to get the content of the text
file, but in your case as the file has only one line its the easiest way to
use
$lines=file('eighty.txt');
to read the content.
[color=blue]
> I will not be adding or removing
> anything from the file, so I will not have to worry about the the index[/color]
getting[color=blue]
> out of order - is this correct? Again, if ",$lines[0]" is optional in the[/color]
above[color=blue]
> example, then do I really need it when writing to the file?[/color]
$lines[0] is not optional. it is required to read your data. | | | | re: How to read file into keyed array?
Okay - it seems to be working as you've described - thanks.
I have another issue with this script I'm wondering if you can help me with. I
want to update certain values in the file based on a time interval. For
example:
$lines=file('eighty.txt');
$numbers=explode('|',$lines[0]);
//pseudo code
$startdate = "03/19/2004"
if today's date is more than one day ahead of $startdate, then:
$numbers[0]=11; //change any number
$output=implode('|',$numbers); //create a string (numbers seperated by | )
$fhandler=fopen('eighty.txt','w');
fwrite($fhandler,$output);
fclose($fhandler);
Basically, I want a conditional clause that prevents $numbers[0] from being
updated until 24 hours after the $startdate. How would I code this? Is
"03/19/2004" proper format for specifying a date? |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|