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

[Beginner] Reading a file into an array (hash)

P: n/a
Hello!

I'm a beginner at PHP and I wonder how I would go about to read a textfile
into an array(hash).

The textfile looks like this:

A/S=age/sex?
A/S/L=age/sex/location?
AA=alcoholics anonymous
AAA=travel club
AAAAA=travel club for alcoholics / american association against acronym
abuse
AAMOF=as a matter of fact
....

It's a file of acronyms, where the first part (before the =) is the acronym
and the second part is the explanation.

I want to read this into an array so that I can access the array with the
acronym and get the explanation. e.g.
$key = 'AAA';
$expl = $acro[$key]

How do I accomplish this in php?

// Anders
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Anders Eriksson wrote:
Hello!

I'm a beginner at PHP and I wonder how I would go about to read a textfile into an array(hash).

The textfile looks like this:

A/S=age/sex?
A/S/L=age/sex/location?
AA=alcoholics anonymous
AAA=travel club
AAAAA=travel club for alcoholics / american association against acronym abuse
AAMOF=as a matter of fact
...

It's a file of acronyms, where the first part (before the =) is the acronym and the second part is the explanation.

I want to read this into an array so that I can access the array with the acronym and get the explanation. e.g.
$key = 'AAA';
$expl = $acro[$key]


Take a look at the function parse_ini_file() at
http://www.php.net/parse_ini_file

Given the PHP file:
<?
$ac = parse_ini_file('testfile.ini');
print_r ($ac);
?>

and the file 'testfile.ini' containing:
A/S=age/sex?
A/S/L=age/sex/location?
AA=alcoholics anonymous
AAA=travel club
AAAAA=travel club for alcoholics / american association against acronym
abuse
AAMOF=as a matter of fact

The results are:
Array
(
[A/S] => age/sex?
[A/S/L] => age/sex/location?
[AA] => alcoholics anonymous
[AAA] => travel club
[AAAAA] => travel club for alcoholics / american association
against acronym abuse
[AAMOF] => as a matter of fact
)
Ken

Jul 17 '05 #2

P: n/a
Hello Ken,

On 3 May 2005 06:49:00 -0700, Ken Robinson wrote:
Anders Eriksson wrote:
I'm a beginner at PHP and I wonder how I would go about to read a

textfile
into an array(hash).

Take a look at the function parse_ini_file() at
http://www.php.net/parse_ini_file

Given the PHP file:
<?
$ac = parse_ini_file('testfile.ini');
print_r ($ac);
?>


Unfortunately can't the parse_ini_file read all characters. As soon as it
finds a ( ) ! " it stops.

So I found an user function that does the same thing and that can read all
characters.

Thank you for your help!

// Anders
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.