Jump to content

explode an array


The Little Guy

Recommended Posts

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

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

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.