By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

How to read file into keyed array?

P: n/a
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!

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"deko" <dj****@hotmail.com> schrieb im Newsbeitrag news:ru*******************@newssvr25.news.prodigy. com... 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!


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
Jul 17 '05 #2

P: n/a
Uzytkownik "deko" <dj****@hotmail.com> napisal w wiadomosci
news:ru*******************@newssvr25.news.prodigy. com...
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?


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);
Jul 17 '05 #3

P: n/a
> 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 | )
5 $fhandler=fopen('eighty.txt','w');
6 fwrite($fhandler,$output);
7 fclose($fhandler);


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?

Jul 17 '05 #4

P: n/a

"deko" <dj****@hotmail.com> schrieb im Newsbeitrag
news:xc*****************@newssvr27.news.prodigy.co m...
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 | ) 5 $fhandler=fopen('eighty.txt','w');
6 fwrite($fhandler,$output);
7 fclose($fhandler);
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] 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;

This assumes the array will be automatically indexed starting at 0. Is this correct? yes, the index of arrays usually starts at 0. 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? 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.
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?


$lines[0] is not optional. it is required to read your data.



Jul 17 '05 #5

P: n/a
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?
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.