Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Can I do this??

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can I do this??

    Hello all,
    Can anyone tell me if I can read a file into an array with the file() function as key and value??

    Let me demonstrate:

    My file looks something like this:

    Code:
    test1|test2
    test3|test4
    test4|test5
    Now I want it to be read as:

    Code:
    $f['test1'] = test2
    $f['test3'] = test4
    $f['test5'] = test6
    Instead of:

    Code:
    $f[0] = test1|test2
    $f[2] = test3|test4
    $f[3] = test5|test6
    Any ideas??

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Do you have to have the file exactly like that. If not take a look at parse_ini_file.
    Also note file() will NOT return
    Code:
    $f[0] = test1|test2
    $f[2] = test3|test4
    $f[3] = test5|test6
    it will return
    Code:
    $f[0] = test1|test2\n
    $f[2] = test3|test4\n
    $f[3] = test5|test6\n

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

    Default

    $f[0] = test1|test2\n
    $f[2] = test3|test4\n
    $f[3] = test5|test6\n
    It won't return that, either. It'll return:
    Code:
    $f[0] = "test1|test2\n";
    $f[1] = "test3|test4\n";
    $f[2] = "test5|test6\n";
    Code:
    $lines = file($filename);
    $config = array();
    for($i = 0; $i < count($lines); ++$i)
      $config[
        substr($lines[$i], 0, strpos($lines[$i], '|'))
      ] =
        substr($lines[$i], strpos($lines[$i], '|') + 1, -1); // -2 for a Windows host
    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!

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    blm126: I don't think I was searching for something like that. I needed the value before the pipe character to be the key and the second after the pipe character to be the value of that array. Thank you for pointing out my mistake.

    Twey: Will it exactly read as the following??

    Code:
    $lines['test1'] = 'test2';
    for a file which is:

    Code:
    test1|test2

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

    Default

    Should do, yep.
    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!

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh thanks Twey!!!

  7. #7
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by shachi
    blm126: I don't think I was searching for something like that. I needed the value before the pipe character to be the key and the second after the pipe character to be the value of that array.
    Parse_ini_file is almost what you want, I think. If you have to use the pipe for the separator it will not work. However, if you can change | to = parse_ini_file is perfect. Take a look at this
    data.ini
    Code:
    test = test2
    test3 = test4
    test.php
    PHP Code:
    print_r(parse_ini_file('data.ini')); 
    will output
    Code:
    Array
    (
    [test] => test2
    [test3] => test4
    )
    I think. I didn't test it(not testing is normally followed by someone proving me wrong, and me looking like an idiot.)
    Last edited by blm126; 08-27-2006 at 07:17 PM.

  8. #8
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    blm126: Yes it is almost what I need but not exactly what I need. I can't change the | to = as I have several places to change if I do so, but I think that now that I know it I may use parse_ini_file() for these purposes.

    Thanks blm126 for helping me with it(and teaching me how to use a new PHP function).

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey, this is how I must use the function isn't it??

    Code:
    <?php
    function newfile($filename){
    $lines = file($filename);
    $config = array();
    for($i = 0; $i < count($lines); ++$i)
      $config[substr($lines[$i], 0, strpos($lines[$i], '|'))] = substr($lines[$i], strpos($lines[$i], '|') + 1, -1); // -2 for a Windows host
    }
    print_r(newfile('newfile.txt'));
    ?>
    or it is something else?? Well if it is this way then why isn't this showing anything??

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

    Default

    Because you put it in a function. If you want to do that, add "return $config;" at the end of the function.
    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
  •