...

View Full Version : Multidimensional array from text file



kyle2k
09-03-2007, 08:05 AM
Hi all,

Im having a problem reading CSV data from a text file and storing it in a multidimensional array.

I have a text file like so.


Cathal|kyle@example.com|83.103.100.300|Wednesday 29th August 2007 @ 17:27-13|66023f4984
tony|tonto@example.com|83.107.82.300|Friday 31st August 2007 @ 12:46-49|1c3fd4594d
steve|imapisces@example.com|86.5.147.300|Friday 31st August 2007 @ 17:22-19|d61f895714


I want to take this data and create a multidimensional array from it.
I have been up all night trying various ways like the one shown below but I cant seem to achive what I need it to do.


$fp1 = fopen ("admin/data/pred.txt", 'rb');
while ( $array = fgetcsv ($fp1, 1024, "|")) {
$multi_array[$array[0]] = array ("$array[0]" => $array);


Any help with this is greatly apreciated.

Regards Kyle :)

Mwnciau
09-03-2007, 08:31 AM
$file = 'file.txt';

$marray = file($file);

foreach($marray as $key => $var){
$marray[$key] = explode('|', $var);
}

kyle2k
09-03-2007, 08:47 AM
Thank you so much Mwnciau, :thumbsup:

kyle2k
09-03-2007, 10:16 AM
Hi again,

Now that I have a multidimentional array from my text file, I have a form that submits a username (the first value from each line of the text file)

What would be the best way to make a normal array or display just that array from the multidimenstional array.

EDIT - What I am trying to achive is search the text file by the username variable (the first value from each line of the text file) and be able to access just that lines info.

Mwnciau
09-03-2007, 11:50 AM
$file = 'file.txt';
$username = 'bob';

$array = file($file);

foreach($array as $key => $var){
$var = explode('|', $var);
if($username == $var[0]){
$user = $var;
break;
}
}
if(!isset($user))
echo 'No user found';
else echo 'User found';

kyle2k
09-04-2007, 05:53 AM
Hi again, and thanks for all your help.

The problem im still having is that when the username is submitted I need to be able to referance just that username's array elements.



$file = 'file.txt';
$username = 'bob';

$array = file($file);

foreach($array as $key => $var){
$var = explode('|', $var);
if($username == $var[0]){
$user = $var;
break;
}
}
if(!isset($user))
echo 'No user found';
else echo 'User found';


foreach ($var as $key => $value){
print "<p>key = $key and value = $value</p>";
}


My foreach still just displays the last array (line of text from file)

Sorry to keep bugging, and I am also trying very hard to work this one out myself, but I cant get my head around this one.

Hope you can understand what im trying to achive.
If this page receives the username via $_GET I want to find a way of having that username's line of info from the text file in an array for further manipulation.

Any help with this is greatly apreciated.

Regards Kyle :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum