java_ftp-er Posted August 27, 2008 Share Posted August 27, 2008 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 More sharing options...
jordanwb Posted August 27, 2008 Share Posted August 27, 2008 Well I can see that on this line: $record = ereg_replace("[^A-Za-z0-9]", " ", $record); You're replacing every alphanumeric character with a space, which I don't think you want. Also why are you using a text file? Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-626519 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 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 Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-626539 Share on other sites More sharing options...
jordanwb Posted August 27, 2008 Share Posted August 27, 2008 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. Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-626561 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 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 Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-626572 Share on other sites More sharing options...
jordanwb Posted August 27, 2008 Share Posted August 27, 2008 One little booboo in your code Ken. $recs is never assigned anything. I hope you understand what he wants to do ken, because I haven't got a clue. Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-626577 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 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 Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-626578 Share on other sites More sharing options...
java_ftp-er Posted August 28, 2008 Author Share Posted August 28, 2008 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? Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-627848 Share on other sites More sharing options...
obsidian Posted August 28, 2008 Share Posted August 28, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/121491-populating-arrays-from-txt-files/#findComment-627895 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.