Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    $file 'file.txt';

    $marray file($file);

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


  • Users who have thanked Mwnciau for this post:

    kyle2k (09-03-2007)

  • #3
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you so much Mwnciau,

  • #4
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    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'

  • #6
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •