Results 1 to 2 of 2

Thread: Multidimensional array from text file

  1. #1
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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.

    Regards Kyle
    Last edited by kyle2k; 09-03-2007 at 10:47 AM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $multi_array = array_map(create_function('$a', 'return explode($a, \'|\');'), file('admin/data/pred.txt'));
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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