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 3 of 3
  1. #1
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool Working with Arrays

    I'm opening up a few text files and insert the contents into a few arrays, and then merging them together - but I'm having difficulty in trying to append some text to the array, here's what I have so far:

    PHP Code:
    <?
    $list1 
    file('1.txt');
    $list2 file('2.txt');
    $list3 file('3.txt');
    $list4 file('4.txt');
    $full=array_merge_recursive($list1,$list2,$list3,$list4);
    shuffle($full);
    ?>
    Basically I read the files into the array, then combine them all, and shuffle up the order. But what I want to do is add some text to each so that I can later on figure out which text file each item came from.

    What I want is something like this:
    PHP Code:
    <?
    $list1 
    file('1.txt');
    //append some text to say it came from file 1.txt
    for (pseudo code){
          
    $list1=$list1 '###1.txt';
    loop}
    ?>
    Of course my actual for loop code doesn't work, because it timesout because it takes too much time to go through each item and append the text. I'm hoping there's a php function that allows you to append text without having to go through each item and append the text.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    you could use the array_map function
    PHP Code:
    function appendString($v) {
      return 
    $v '##.txt';
    }
    $list1 file('1.txt');
    $list  array_map('appendString'$list1); 
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    BatCountry (11-09-2007)

  • #3
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts
    n/m..I read the code wrong..great work!

    [strike]that looks interesting, but won't that overwrite the contents of the array?
    Currently my array contains something like this:
    Somtext##anotherbitofdata

    and I want to append the textfile name, so that it looks like this:
    Somtext##anotherbitofdata#1.txt

    with the function you gave, I think it would overwrite the values? to just the text file name?[/strike]
    Last edited by BatCountry; 11-09-2007 at 07:26 AM.


  •  

    Posting Permissions

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