• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Reading a text file/php file into an associative array

suklee

Diamond Member
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?
 
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...
 
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);
?>
 
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.
 
Awesome, thanks so much to both of you!

:beer: for jjones
:beer: for George

+small :beer: as well to mundane as well for trying 🙂
 
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:
 
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. 😀 :beer:
 
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);
?>
 
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);
?>
 
Back
Top