Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-03-2007, 08:05 AM   PM User | #1
kyle2k
New to the CF scene

 
Join Date: Sep 2007
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
kyle2k is an unknown quantity at this point
Multidimensional array from text file

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.
Code:
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.
Code:
$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
kyle2k is offline   Reply With Quote
Old 09-03-2007, 08:31 AM   PM User | #2
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
PHP Code:
$file 'file.txt';

$marray file($file);

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

Mwnciau is offline   Reply With Quote
Users who have thanked Mwnciau for this post:
kyle2k (09-03-2007)
Old 09-03-2007, 08:47 AM   PM User | #3
kyle2k
New to the CF scene

 
Join Date: Sep 2007
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
kyle2k is an unknown quantity at this point
Thank you so much Mwnciau,
kyle2k is offline   Reply With Quote
Old 09-03-2007, 10:16 AM   PM User | #4
kyle2k
New to the CF scene

 
Join Date: Sep 2007
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
kyle2k is an unknown quantity at this point
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.

Last edited by kyle2k; 09-03-2007 at 10:49 AM..
kyle2k is offline   Reply With Quote
Old 09-03-2007, 11:50 AM   PM User | #5
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
PHP Code:
$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'
Mwnciau is offline   Reply With Quote
Old 09-04-2007, 05:53 AM   PM User | #6
kyle2k
New to the CF scene

 
Join Date: Sep 2007
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
kyle2k is an unknown quantity at this point
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.

Code:
$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
kyle2k is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:41 AM.


Advertisement
Log in to turn off these ads.