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
    Join Date
    May 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    explode array question

    Hi,

    I have a string, like this:

    Userid1 Pass1
    Userid2 Pass2
    Userid3 Pass3
    Userid4 Pass4

    Each column is tab separated. I want to explode it into an array so that the userid will be the key, and the password the value. I tried to do it like this:

    $array = explode("\t", $string); // Where $string is the string above.

    However, this returned both the userid and the password as the value, and created an automatic key for each row in the array. Can anyone help me?

    Thanks,

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You can explode the string with the newline character "\n" to separate the lines, and then loop through it and separate the string with "\t". Something like this:
    PHP Code:
    $str 'user1    pass1
    user2    pass2
    user3    pass3'
    ;

    $array explode("\n"$str);
    foreach(
    $array as $k => $v)
    {
        unset(
    $array[$k]);
        
    $v explode("\t"$v);
        
    $array[$v[0]] = $v[1];


  • #3
    New Coder
    Join Date
    May 2006
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks man, that's exactly what I was looking for!


  •  

    Posting Permissions

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