The Little Guy Posted February 21, 2007 Share Posted February 21, 2007 For my PHP class, I need to make a flat file database, I need to search the database and sort all the records, so here is what Im doing (question lies within the code): <?php $user = $_POST['username']; $pass = $_POST['password']; $handle = fopen("../../login.txt","r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle,4096); # Explode each line of the file to make an array $value = explode("\n",$buffer); # For each line, I need to break down all the values which are seperated by a : # My problem lies here, Can I explode a value in an array? foreach($value as $record){ $val = $record; $rec = explode(":",$val); } print_r($rec); } fclose($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/39526-explode-an-array/ Share on other sites More sharing options...
Barand Posted February 21, 2007 Share Posted February 21, 2007 Yes, you can. The print_r() needs to be inside the foreach {} loop or you will only print the final record. Have a look at the file() function $lines = file ("../../login.txt"); foreach ($lines as $value) { $rec = explode (':', $value); print_r ($rec); echo '<br>'; } Link to comment https://forums.phpfreaks.com/topic/39526-explode-an-array/#findComment-190741 Share on other sites More sharing options...
itsmeArry Posted March 1, 2007 Share Posted March 1, 2007 echo "<pre>"; print_r ($rec); this will print formatted array Link to comment https://forums.phpfreaks.com/topic/39526-explode-an-array/#findComment-196951 Share on other sites More sharing options...
boo_lolly Posted March 1, 2007 Share Posted March 1, 2007 you will basically end up with a 2-dimensional array. the first indices will be each line. and within each indices will be a nested array, with each exploded piece of the line in its own indices. does that make sense? Link to comment https://forums.phpfreaks.com/topic/39526-explode-an-array/#findComment-196961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.