Jump to content

populating arrays from .txt files


java_ftp-er

Recommended Posts

I am attempting to use PHP  5 to populate an array from Text file.

Unfortunately my array does not seem to populate (empty) even though a var_dump() shows that the data is being correctly retrieved from the text file.  code below:

     $file = "technologies.txt";
     $id;
     $tech;
     $tech_area; 
     
        $data_array = file($file); 
        //if(!is_array($data_array)) { return ''; }
        $count = 0;
        foreach($data_array as $record)
        {
            $record = ereg_replace("[^A-Za-z0-9]", " ", $record);
            $tech_array = explode(",",$record);
            //echo var_dump($tech_array);
            $id[$count] = $tech_array[0];
            $tech[$count] = $tech_array[1];
            $tech_area[$count] = $tech_array[2];
            echo "<p>$tech[$count] $count</p>";
            echo "<p>$tech_array[1]</p>";
            $count += 1;
        }

the text file approximately has 100 lines similar to the following example:

10,Java APIs,JAX-WS

$id,$tech_area,$tech are all empty after running this code -- does anyone know what's wrong?

any help greatly appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/
Share on other sites

What do you think these three lines do?

<?php
     $id;
     $tech;
     $tech_area; 
?>

 

If you want to initialize empty arrays, do:

<?php
     $id = array();
     $tech = array();
     $tech_area = array();
?>

 

Ken

 

What do you think these three lines do?

<?php
     $id;
     $tech;
     $tech_area; 
?>

 

 

Yeah I noticed that too but didn't bother saying anything. Also the explode line seems a little strange:

 

PHP says:

Description

array explode ( string $delimiter , string $string [, int $limit ] )

 

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter .

 

Also on the foreach line the "as $record" the $record is an index, an integer not a string.

Here's one solution:

<?php
$id = array();
$tech = array();
$tech_area = array();
$data_array = file($file);
foreach ($recs as $rec)
list($id[],$tech[],$tech_area[]) = explode(',',trim($rec));
echo '<pre>' . print_r($id,true) . print_r($tech,true) . print_r($tech_area,true) . '</pre>';
?>

 

Ken

That's what I get for combining my test code with the OP's code. This should be correct:

<?php
$id = array();
$tech = array();
$tech_area = array();
$data_array = file($file);
foreach ($data_array as $rec)
list($id[],$tech[],$tech_area[]) = explode(',',trim($rec));
echo '<pre>' . print_r($id,true) . print_r($tech,true) . print_r($tech_area,true) . '</pre>';
?>

 

Ken

Thanks all.  Great advice.  I ended up with this which works fine-

 

    $file = "technologies.txt"

    $id = array();

    $tech_area = array();

 

    $data_array = file($file);

    $count = 0;

    foreach ($data_array as $rec) {

        $rec = ereg_replace('[/"]',"", $rec);

        list($id[],$tech[],$tech_area[]) = explode(',',trim($rec));

        $count += 1;

    }

 

By the way I put the wrong regular expression in my first post-sorry for the confusion.

 

And thanks for the information about initialising the arrays -- I didn't think that was necessary in PHP.  I have used arrays before without initialising them -- what is the benefit of initialising them in this way?

There is a function to help you with reading CSV files in PHP, so you don't have to do it all from scratch unless you really want to. Look into this as a possible alternative, too:

<?php
$id = array();
$tech = array();
$tech_area = array();

$handle = fopen("technologies.txt", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $id[] = $data[0];
    $tech[] = $data[1];
    $tech_area[] = $data[2];
}
fclose($handle);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.