Reading a text file/php file into an associative array

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
This is really simple... but I need some help.

Say I have a file with the following contents stored as abc.txt...


I want to read it into an associative array A=>A, B=>B, C=>C...

I can get it into an array like so:

<?php
$filename = 'abc.txt';
$fp = @fopen($filename, 'r');
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
?>

But how do I change this into an associative array?
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
Second question: what if my text file was so:

A,Albert
B,Bob
C,Charlie

And I wanted to read it into an associative array?
A => Albert
B => Bob
C => Charlie
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
For post #2... I used this code:

<?php

$row = 1;
$handle = fopen("abc.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
$key = $data[$c];
}
}
fclose($handle);

?>

It prints out:

A
Albert
B
Bob
C
Charlie

But I can't figure out how to capture this into an assoc. array...
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Wouldn't it be something like this:

<?php

$array = array();
$handle = fopen("abc.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$array[$data[0]] = $data[1];
}
fclose($handle);

print_r($array);
?>
 
Aug 25, 2004
11,151
1
81
data.txt:

A,Albert
B,Bob
C,Charlie

file.php

<?

$data = array(); //this is where data from data.txt will be stored

$handle = fopen("data.txt,"r") or die("EPIC FAIL!");

while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[$row[0]] = $row[1];
}

fclose($handle);

?>

This will work only if your data file is in csv format.
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
Awesome, thanks so much to both of you!

:beer: for jjones
:beer: for George

+small :beer: as well to mundane as well for trying :)
 
Aug 25, 2004
11,151
1
81
Originally posted by: Kai920
Awesome, thanks so much to both of you!

:beer: for jjones
:beer: for George

+small :beer: as well to mundane as well for trying :)

LOL, I just realized jjones beat me to the punch. Respect :thumbsup:
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Originally posted by: George P Burdell
Originally posted by: Kai920
Awesome, thanks so much to both of you!

:beer: for jjones
:beer: for George

+small :beer: as well to mundane as well for trying :)

LOL, I just realized jjones beat me to the punch. Respect :thumbsup:

Yeah, but kudos to you for using Epic Fail in your code. :D :beer:
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
Hey,

What if my file is no longer CSV, but just a text file: (See original post)

category.txt:

Apples
Bananas
Oranges

And I wanted to read this into an associative array as well:

Apples => Apples
Bananas => Bananas
Oranges => Oranges?

I used this code, but if there's a more efficient way (probably is), please let me know!

<?php
$filename = 'category.txt';
$fp = @fopen($filename, 'r');
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}

print_r($array);

echo "<br />";

$c = 0;

while ($c < count($array)) {
$Category[($array[$c])] = $array[$c];
$c++;
}

print_r($Category);
?>
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Same code will work:

<?php

$array = array();
$handle = fopen("category.txt", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$array[$data[0]] = $data[0];
}
fclose($handle);

print_r($array);
?>