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,

    Well, unfortunately you'll have to do it in two steps: (1) Get the results into an array, (2) Construct the array you want....

    It'd look something like:

    <?php
    
    $tmp = explode("\t", $string); // Where $string is the string above.
    for($i=0, $j=sizeof($tmp); $i<$j;$i++) {
      $users[$tmp[$i]] = $tmp[++$i];
    }
    
    ?>
      <pre>
      <?php
      $text = "Userid1\tPass1
      Userid2\tPass2
      Userid3\tPass3
      Userid4\tPass4
      ";
      $lines = preg_split('/[\r\n]+/', $text, 100, PREG_SPLIT_NO_EMPTY);
      foreach($lines as $line)
      {
         list($user, $pwd) = explode("\t", $line);
         $userData[$user] = $pwd;
      }
      // show result:
      print_r($userData);
      ?>
      </pre>
      
        Write a Reply...