SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Create and write an array to a file

    Could anyone help me put the following information into an array and then write that array instantly to a file (as opposed to looping through it - if possible)?

    The data (to be separated by |) is:
    row1 tim sean dan mick tom
    row2 mary helen
    row3 mike kelly fred dan liam dave jim ....(unlimited number - the data size could be large here!)


    I have tried to do this for several hours but all that gets written to my file is the word "Array"!!

    Thanks,

    Willie

  2. #2
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, let's say you have something like this
    PHP Code:
    $thearray[0] = 'tim sean dan mick tom';
    $thearray[1] = 'mary helen';
    $thearray[2] = 'mike kelly fred dan liam dave jim ....'
    So if you want to save the entire content of this array into a file, putting a | to separate each row then use,
    PHP Code:
     $data implode('|',$thearray);
     
    /*
     This will produce
     $data = 'tim sean dan mick tom|mary helen|mike kelly fred dan liam dave jim ....';
     */ 
    Now, open a file for writing and insert $data, and that's it
    PHP Code:
     $fh fopen('filename'.'w')
     
    fwrite($fh,$data);
     
    fclose($fh); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply,

    My plan was to print the following to the file:

    row1|tim|sean|dan|mick|tom|/n
    row2|mary|helen|/n
    row3|mike|kelly|fred|dan|liam|dave|jim|....|/n

    This is a bit different to what your code would do! Any ideas on how to sort me out?

    Willie

  4. #4
    SitePoint Addict rwar's Avatar
    Join Date
    Sep 2001
    Location
    PF / RS / BR
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use something like this (use the code you gave too):

    $string = str_replace(" ","|",$string);

    Use this before you save you data to a file.
    php? mysql? apache? That's it.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not so sure that what you said would work. Yes, the '|' are inserted but I feel that the file would not be written to correctly.

    File (text.txt)
    row1|tim|sean|dan|mick|tom|/n
    row2|mary|helen|/n
    row3|mike|kelly|fred|dan|liam|dave|jim|....|/n

    For instance, how would you apply what you said where one reads the data in text.txt into a multidimensional array, then you change kelly to say dick and then you write back the full array to the file.

    This should be such a simple exercise yet I agree with earlier comments I read regarding the complexity of the PHP manual!

    Willie

  6. #6
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why not serialize() it when inserting and unserialize() when getting it?
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea on how to go about that!

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array help needed

    Would anyone have some time to help me out on this one?

  9. #9
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand you, you've got a file text.txt like
    PHP Code:
    |tim|sean|dan|mick|tom|(end of line)
    |
    mary|helen|(end of line
    |
    mike|kelly|fred|dan|liam|dave|jim|....|(end of line
    and you want to insert the content into a multidimensional array. Try the following code and tell me if that's what you exactly are looking for:
    PHP Code:
     $content = @file('text.tx');
     
     foreach (
    $content as $line)
        
    $data[] = explode('|',$line); 
    that will produce
    $data[0] = array('','tim','sean','dan','mick ...
    $data[1] = array('','mary',...
    and so on.

    To change 'kelly' to say 'dick', then
    PHP Code:
      echo $data[2][1]; /* this print out kelly */
      
    $data[2][1] = 'dick'
    Paul

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul,

    Yes you are on the right track.

    Now the final step that is needed is to write the updated contents back to the file text.txt - this could be difficult!

    Willie

  11. #11
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by waherne
    I have no idea on how to go about that!
    If your array is $array - just do this to write the array to file:

    $data = serialize($array);
    $fh = fopen('filename'.'w')
    fwrite($fh,$data);
    fclose($fh);

    and this to get the array:

    $data = file('filename');
    $array = unserialize($data);
    I have not tested that, but it should work like that - I belive.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  12. #12
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by waherne
    Thanks Paul,

    Yes you are on the right track.

    Now the final step that is needed is to write the updated contents back to the file text.txt - this could be difficult!

    Willie
    Come on!! I've already given you the clue, check:
    PHP Code:
     foreach ($data as $a) {
       
    $line implode('|',$a);
       
    $text .= "$a\n";
     }

     
    $fh fopen('filename','w')
     
    fwrite($fh,$text);
     
    fclose($fh); 
    Last edited by Paul S; Nov 13, 2001 at 19:15.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Paul,

    I actually got is sorted prior to your most recent post! I will close out this thread later with the final code for others to see.

    I'm sure that I tried something like your code before and that it resulted in the word "Array" being printed to the file - maybe I am mistaken.

    In any case, what matters most is the support you have given and many thanks for that, and of course, the fact that my script now works.

    Willie


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
  •