/    Sign up×
Community /Pin to ProfileBookmark

How to Read Flat File into Multi Array

I’ve written a bunch of program over the years, but only recently started using PHP. I’m currently converting some ancient QuickBASIC educational programs so they can be Web-accessible. Here’s the problem:

I have a flat file of animals and genetic traits. The following is a sample:

[CODE]hamsters1
brown eyes
blue eyes
pink eyes
smooth hair
rough hair
broken hair
golden hair
black hair
large ears
small ears
[/CODE]

The file continues on like the above. There are six different animal species, four sets of each species and 10 traits for each species.

I tried a somewhat direct translation of the QuickBASIC to PHP, but it doesn’t work:

[CODE]$file = fopen(“allanims.txt”, “r”);
for ($i = 1; $i <= 6; $i++) {
for ($j = 1; $j <= 4; $j++) {
$species[$i][$j] = fgets($file);
for ($k = 1; $k <= 10; $k++) {
$trait[$i][$j][$k] = fgets($file);
}}}
fclose($file);
[/CODE]

I’ve read a lot of stuff about multi-dimensional arrays in PHP, but all the examples involve hard coding the array contents, rather than reading them from a file.

Lane

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@chrisranjanaMar 18.2012 — Do you want to read each character as a php array element or each word as a php array element ?
Copy linkTweet thisAlerts:
@hyperionXSMar 18.2012 — Try this
[code=php]
$species = file('allanims.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
[/code]
Copy linkTweet thisAlerts:
@LaneLesterauthorMar 18.2012 — Thanks, guys. I want each line to be a separate array item, and I want to end up with two arrays, $species and $traits, out of the one list.

I remembered an old trick for having pseudo-multi-dimensional arrays in languages that don't support multi's. I think this code is going to work:
[CODE] for ($i = 1; $i <= 6; $i++) {
for ($j = 1; $j <= 4; $j++) {
$ij = $i * $j;
$species[$ij] = fgets($file);
for ($k = 1; $k <= 10; $k++) {
$ijk = $ij * $k;
$trait[$ijk] = fgets($file);
}}}[/CODE]

Lane
Copy linkTweet thisAlerts:
@ZABIMar 19.2012 — [code=php]$array = explode("rn",file_get_contents('file.txt'));[/code]
should work for you
Copy linkTweet thisAlerts:
@007JulienMar 19.2012 — After Zabi explode, with this scheme

[CODE]
species_0 rank 0
animal_0 rank 1
trait_0 rank 2
...
trait_9 rank 11

animal_1 rank 12
trait_0 rank 13
...
trait_9 rank 22

animal_2 rank 23
trait_0 rank 24
...
trait_9 rank 33

animal_3 rank 34
trait_0 rank 35
...
trait_9 rank 44

species_1 rank 45[/CODE]
The species rank are (1 species, 4 animals, 40 traits = 45) 45*k and the animals 45*k+1+11*a Then it's possible to get the species with a &#37;45 with something like this :

[CODE]$species=array();
$animalsBySpecies=array();
$traitsByAnimals=array();

foreach ($array as $r=>$s) if ($r%45==0) {//$s is a new species
$species[]=$s;$animalsBySpecies[$v]=array();
for ($i=0;$i<4;$i++) {$a=$array[$k+1+11*$i];// an animal
$animalsBySpecies[$v][]=$a;$traitsByAnimals[$a]=array();
for ($j=0;$j<10;$j++) {$traitsByAnimals[$a][]=[$k+1+11*$i+1+$j];}
}
}
[/CODE]
I hope not to have made error !
×

Success!

Help @LaneLester spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...